in reply to Re^2: references--hard vs anonymous operational weirdness
in thread references--hard vs anonymous operational weirdness
Quite right, I'd got caught by an optimization. I ran (adjusting code to your example)
The same address even though @array is declared in the loop. The variable is being reused. Assigning \@array to something prevents the optimization.for my $tab ( 1 .. 3 ) { my @array = frobnicate($tab); printf "Array ref: %s\n", \@array; printf "Annoymous: %s\n", [@array]; } sub frobnicate { return ( 0 .. shift ); } __END__ Array ref: ARRAY(0x826a924) Annoymous: ARRAY(0x8183a54) Array ref: ARRAY(0x826a924) Annoymous: ARRAY(0x826d818) Array ref: ARRAY(0x826a924) Annoymous: ARRAY(0x82115f8)
Something to bear in mind when benchmarking.my %data; for my $tab ( 1 .. 3 ) { my @array = frobnicate($tab); printf "Array ref: %s\n", \@array; printf "Annoymous: %s\n", [@array]; $data{$tab} = \@array; } sub frobnicate { return ( 0 .. shift ); } __END__ Array ref: ARRAY(0x826a924) Annoymous: ARRAY(0x8183a54) Array ref: ARRAY(0x819f318) Annoymous: ARRAY(0x8183798) Array ref: ARRAY(0x826ade8) Annoymous: ARRAY(0x819f304)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: references--hard vs anonymous operational weirdness
by chromatic (Archbishop) on Mar 23, 2008 at 03:55 UTC | |
by hipowls (Curate) on Mar 23, 2008 at 04:19 UTC | |
by ikegami (Patriarch) on Mar 23, 2008 at 04:41 UTC | |
by chromatic (Archbishop) on Mar 23, 2008 at 05:44 UTC | |
by ikegami (Patriarch) on Mar 23, 2008 at 06:01 UTC | |
by hipowls (Curate) on Mar 24, 2008 at 10:02 UTC | |
by ikegami (Patriarch) on Mar 24, 2008 at 18:34 UTC | |
|
In Section
Seekers of Perl Wisdom