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.


In reply to Re: Difference between this array assignment and push by kyle
in thread Difference between this array assignment and push by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.