Scalar values ββin Perl can be a number and a string at the same time. An SV object (SV = Scalar Value) has slots for integer, floating, and string values ββand flags that determine which of these values ββare valid at any given time. When you use a string value, perl evaluates the string value and sets a flag that identifies it as valid. (Other operations, such as appending 1, invalidate the string value.) When you print something that you (not surprisingly) use as a string. You can see this with Devel :: Peek.
use Devel::Peek; my $s = '6.1.0.7 (build 25.3.1103030000)'; if ($s =~ /^\s*([0-9.]*) \(build.*\)/) { Dump($1); printf STDERR "\$1 = $1\n"; Dump($1); }
Result
SV = PVMG(0x1434ca4) at 0x144d83c REFCNT = 1 FLAGS = (GMG,SMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x146c324 MG_VIRTUAL = &PL_vtbl_sv MG_TYPE = PERL_MAGIC_sv(\0) MG_OBJ = 0x144d82c MG_LEN = 1 MG_PTR = 0x14631c4 "1" $1 = 6.1.0.7 SV = PVMG(0x1434ca4) at 0x144d83c REFCNT = 1 FLAGS = (GMG,SMG,pPOK) IV = 0 NV = 0 PV = 0x1487a1c "6.1.0.7"\0 CUR = 7 LEN = 8 MAGIC = 0x146c324 MG_VIRTUAL = &PL_vtbl_sv MG_TYPE = PERL_MAGIC_sv(\0) MG_OBJ = 0x144d82c MG_LEN = 1 MG_PTR = 0x14631c4 "1"
Notice that in the second output of the dump, the PV slot (string value) was filled and the pPOK flag was added to FLAGS.
So yes, print has side effects, although under normal circumstances you will never notice. version->parse() seems to expect a string argument, but does not invoke string semantics. Given that version prefers to use the XS implementation, this is probably a bug, not perl. Note that making a copy of the capture data causes the problem to disappear:
use Data::Dump qw'pp'; my $s = '6.1.0.7 (build 25.3.1103030000)'; if ($s =~ /^\s*([0-9.]*) \(build.*\)/) { my $x = $1; pp(version->parse($x)); }
Result:
bless({ original => "6.1.0.7", qv => 1, version => [6, 1, 0, 7] }, "version")
source share