in reply to Converting an Array Reference into an Array

A quick look with B::Terse (hey, I learned a new trick, I'm gonna use it :) ) at this:
my $aryref=[ qw(one two) ]; my @array=@$aryref; my @arraytwo=qw(one two);
Shows this as a result:
LISTOP (0x8115698) leave [1] OP (0x8120b78) enter COP (0x8115658) nextstate BINOP (0x8115630) sassign UNOP (0x81795e0) srefgen UNOP (0x8120cd8) null [141] LISTOP (0x8120340) anonlist OP (0x8120cb8) pushmark SVOP (0x811a068) const PV (0x811d8e4) "one" SVOP (0x81203c0) const PV (0x811d8d8) "two" OP (0x814db90) padsv [1] COP (0x81157d0) nextstate BINOP (0x8115770) aassign [4] UNOP (0x8115728) null [141] OP (0x8115750) pushmark UNOP (0x81156c0) rv2av [3] OP (0x8179600) padsv [1] UNOP (0x81156e0) null [141] OP (0x8115708) pushmark OP (0x81795c0) padav [2] COP (0x8120b38) nextstate BINOP (0x8120b10) aassign [6] UNOP (0x8120a80) null [141] OP (0x8120aa8) pushmark SVOP (0x8120a40) const PV (0x811d908) "one" SVOP (0x8120a60) const PV (0x811d8f0) "two" UNOP (0x8120ac8) null [141] OP (0x8120af0) pushmark OP (0x8115810) padav [5] cloning.pl syntax OK
In other words, the @array=@$aryref does not copy the array members, it just sets up @array to point to the data pointed to by $aryref (and increases said data's refcount). Perl does the right thing here. So, you should not expect a performance penalty for this. You should still have some penalty (notably, pointing the new array's members to the SV's), but not the same as creating a full blown array from the ground up. Ah well, rv2av is apparently a full blown copy. Never mind my blabbering. Thanks chromatic and PodMaster.

However, I would try and get used to the 'ugly syntax' of references and dereferencing them - they are among the most powerful features of Perl and are the only way of building complex data structures. You will need them :)

Update: Stopped lying. :)

CU
Robartes-

Replies are listed 'Best First'.
Re: Re: Converting an Array Reference into an Array
by chromatic (Archbishop) on Mar 12, 2003 at 08:01 UTC

    This is incorrect. rv2av dereferences an array reference. Search for pp_rv2av in pp_hot.c.

Re: Re: Converting an Array Reference into an Array
by PodMaster (Abbot) on Mar 12, 2003 at 08:01 UTC
    That is not accurate.
    my $coy = [ 1, 2]; my @coy = @$coy; $coy[1] = 666; use Data::Dumper; die Dumper( $coy, \@coy ); __END__ $VAR1 = [ 1, 2 ]; $VAR2 = [ 1, 666 ];
    Don't ask me to explain, I don't know Terse (and may be some "copy on write" magic going on).


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.