Perl - pass by reference v pass by value

Paul Webster <Paul.Webster.a8bjuo-NUepA2SMhDQqspMVqqL2D+4xXEVPTSb/[email protected]>
Newsgroups gmane.music.equipment.slimdevices.devel
Organization Logitech Squeezebox Forums
Message-ID <[email protected]>
In the old days when I was a programmer in a long forgotten language
(Coral-66) it was really clear if you were using a value or a
reference/location when working with stuff.

I have just been caught out with this in Perl and would like to know the
correct way to make the setting of variable $newvalue below to be a
value and not a reference.

Here is example code:

Code:
--------------------
    
  use Data::Dumper;
  my $dumped;
  
  my $variable1 = {
  subarray => ['abc1', 'abc2']
  };
  
  
  $dumped = Dumper $variable1;
  print "Original: $dumped\n";
  
  foreach my $newvalue ( @{$variable1->{subarray}} ){
  print "subarray value: $newvalue\n";
  $newvalue = 'a new string';
  }
  
  $dumped = Dumper $variable1;
  print "Original has been changed: $dumped\n";
  
--------------------


and here is the output

Code:
--------------------
    
  Original: $VAR1 = {
  'subarray' => [
  'abc1',
  'abc2'
  ]
  };
  
  subarray value: abc1
  subarray value: abc2
  Original has been changed: $VAR1 = {
  'subarray' => [
  'a new string',
  'a new string'
  ]
  };
  
--------------------


I did not expect that the values in $variable1 to change through me
changing a new local variable $newvalue.



Paul Webster
http://dabdig.blogspot.com
author of \"now playing\" plugins covering radio france (fip etc),
planetradio (bauer - kiss, absolute, scala, jazzfm etc), kcrw, supla
finland, abc australia, cbc/radio-canada and rte ireland
------------------------------------------------------------------------
Paul Webster's Profile: http://forums.slimdevices.com/member.php?userid=105
View this thread: http://forums.slimdevices.com/showthread.php?t=115171
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.