I'm confused about perl subroutine parameters in this example
when i use references in subroutine parameters it works:
@a = ( 1, 2 );
@b = ( 5, 8 );
@c = add_vecpair( \@a, \@b );
print "@c\n";
print $a[0];
sub add_vecpair { # assumes both vectors the same length
my ( $x, $y ) = @_; # copy in the array references
my @result;
@$x[0] = 2;
for ( my $i = 0; $i < @$x; $i++ ) {
$result[$i] = $x->[$i] + $y->[$i];
}
return @result;
}
but when i don't use references as parameters like this:
@a = ( 1, 2 );
@b = ( 5, 8 );
@c = add_vecpair( @a, @b );
print "@c\n";
print $a[0];
sub add_vecpair { # assumes both vectors the same length
my ( @x, @y ) = @_; # copy in the array references
my @result;
print @y;
for ( my $i = 0; $i < @x; $i++ ) {
$result[$i] = $x[$i] + $y[$i];
}
return @result;
}
..it doesn't work. When do i need to use references as subroutine parameters?