in reply to Copying an array or hash

If you pass an array or a hash, then modifying it should not modify the original.

my @foo = qw(x y z); bar(@foo)

Update: Boy, do I feel like a fool.

Of course, if you pass as a reference...

my @foo = qw(x y z); bar(\@foo)

...or if you are dealing with a reference in the first place, then the original will be modified. To avoid this, you can dereference the array (or hash) within the subroutine. How you do that is described in perldoc perlref

Replies are listed 'Best First'.
Re^2: Copying an array or hash
by Fletch (Bishop) on Dec 05, 2005 at 14:11 UTC
    If you pass an array or a hash, then modifying it should not modify the original.
    use YAML qw( Dump ); sub yarly { $_[0] = "O RLY?" } my @a = ( "b", "c", "d" ); print Dump( \@a ), "\n"; yarly( @a ); print Dump( \@a ), "\n"; __END__ --- #YAML:1.0 - b - c - d --- #YAML:1.0 - O RLY? - c - d

    Elements of @_ are aliases for the originals.