and had to thump myself with a cluestick when I realized why. $2 is a magic variable that fetches the 2nd capture group from the last matched regex in scope. But that magic comes at a cost in storage; as vaguely shown in http://search.cpan.org/perldoc/B#SV-RELATED_CLASSES, a magic variable (a PVMG or subclass thereof) has fields to not only store a string, but also an integer, a floating point value, and in addition, a pointer to a list of magic that applies to this variable. And when perl does an assignment, the target scalar is upgraded to allow it to store at least as much info as the source scalar could, whether or not it actually needs to. So, in my example, all the hash values are also PVMG's, though only the PV (string) fields are actually used.use Test::More "no_plan"; use Devel::Size "total_size"; my $key = "aa"; my $val = "a00"; my %hash1; my %hash2; while (length($key) == 2) { $hash1{$key} = $val; "$key$val" =~ /(..)(...)/ and $hash2{$1} = $2; ++$key; ++$val; } is(keys(%hash1), keys(%hash2), "same number of keys"); is_deeply(\%hash1, \%hash2, "is_deeply same"); is(total_size(\%hash1), total_size(\%hash2)); __END__ ok 1 - same number of keys ok 2 - is_deeply same not ok 3 # Failed test (sizer.pl at line 18) # got: '39316' # expected: '58244' 1..3 # Looks like you failed 1 test of 3.
Making $2 be "$2" makes the hash values simple PV types, and makes all tests pass.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: $1, etc. are not just strings
by syphilis (Archbishop) on Jan 01, 2007 at 12:30 UTC | |
|
Re: $1, etc. are not just strings
by diotalevi (Canon) on Jan 02, 2007 at 01:13 UTC | |
|
Re: $1, etc. are not just strings
by ikegami (Patriarch) on Jan 04, 2007 at 16:49 UTC | |
|
Re: $1, etc. are not just strings
by gam3 (Curate) on Jan 04, 2007 at 16:36 UTC | |
|
Re: $1, etc. are not just strings
by OfficeLinebacker (Chaplain) on Jan 18, 2007 at 02:42 UTC | |
by diotalevi (Canon) on Jan 18, 2007 at 04:27 UTC | |
by OfficeLinebacker (Chaplain) on Jan 18, 2007 at 04:54 UTC | |
by ysth (Canon) on Jan 18, 2007 at 06:17 UTC |