Shallow copy reference into variable in Perl -


in perl, can assign variable reference variable, this:

my @array = (1..10); $ref = \@array; 

and, reference, can , both variables affected:

push @array, 11; push @$ref, 12; 

and both variables contain 1..12, because both point same space.

now, i'd know if there way can same thing, starting ref , later assigning reference plain variable. example:

my $ref = [1..12]; @array = # here makes @array point same space $ref contains 

i know can assign this:

my @array = @$ref; 

but, that's copy. if alter $ref or @array, independent changes.

is there way make @array point same variable $ref?

four ways:

  • our @array; local *array = $ref;
  • \my @array = $ref; (experimental feature added 5.22)
  • use data::alias; alias @array = @$ref;
  • using magic (e.g. tie @array, 'tie::stdarrayx', $ref;)

but of course, sensible approach do

my $array = $ref; 

and use @$array instead of @array.



aforementioned tie::stdarrayx:

package tie::stdarrayx; use tie::array qw( ); our @isa = 'tie::stdarray'; sub tiearray { bless $_[1] || [], $_[0] } 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -