2011年7月11日 星期一

string call by ref/value in foreach/function

use strict;
sub modifyString
{
    my @param=@_;
   
    foreach (@param)
    {
        $_ =~ s/aaa/bbb/;
        print "param member =$_\n";
    }
    print "param[0] out of loop=$param[0], foreach directly modify member\n";
    return $param[0];   
}

my $string1="aaa";
my $string2="aaa";
modifyString($string1);
$string2 = modifyString($string2);
print "string1 =$string1 will restore to its original value (not call by reference)\n";
print "string2 =$string2 (only if you assign by functoin return value, the string changes\n";

 

image

沒有留言: