One difference is that the first creates a smaller array. This is because the $1 variable is special and carries a little extra special data with it. When you stuff it into an array, that stuff stays. (I remember this fact without remembering the details. Perhaps a monk more familiar with Perl internals could explain better.)
use Data::Dumper;
use Devel::Size qw(total_size);
my $bigfoo = 'foo' x 1_000;
my @assigned = $bigfoo =~ /(foo)/g;
my @pushed;
push @pushed, $1 while $bigfoo =~ /(foo)/g;
printf "pushed: %d\n", total_size( \@pushed );
printf "assigned: %d\n", total_size( \@assigned );
if ( Dumper( \@pushed ) eq Dumper( \@assigned ) ) {
print "They look the same.\n";
}
else {
print "They look different.\n";
}
__END__
pushed: 52132
assigned: 32052
They look the same.
The effect is lessened if you push @pushed, "$1":
pushed: 32132
assigned: 32052
Even then @pushed still comes out larger, probably because it started small and grew through the loop while @assigned was the right size to begin with.
My guess would be that the assigned method is faster too (especially with the repeated calls to $mech->content), but I haven't tested that. |