my @y= (undef)= (3,4);
####
sub set {
my( $pos, $newval )= @_;
$_[$pos]= $newval;
return $_[$pos];
}
print set( 2, "hello", undef ), $/; # prints "hello"
my $x= "one";
print set( 2, "two", $x ), $/; # prints "two", changes $x
print $x, $/; # prints "two"
print set( 2, "four", $x.3 ), $/; # prints "for", $x unchanged
print $x, $/; # prints "two"
print set( 2, "hello", "goodbye" ); # dies with:
# Modification of a read-only value attempted
####
($a,undef,$b)= @x; # [undef] is copied
($a,"hi",$b)= @x; # "hi" is aliased and so this dies
@x= ($a,undef,$b); # [undef] is copied so $x[1] is modifiable
@x= ($a,"hi",$b); # "hi" is copied so $x[1] is modifiable