My understanding from PERLVARS is that the '@_' variable is the list of parameters supplied to a subroutine. My understanding is that the 'shift' function pops the first value from that list, but that the elements of @_ can also be accessed directly, as with any other list. The problem I am seeing is that when a subroutine is called more than once, the @_ list always contains the values from the very first call, and the values don't seem to be updated.
#!/usr/bin/perl
#Test the shift command
foreach(1..5) {
print "Input: $_ ";
&Test_Shift($_);
}
#Test the @_ list
foreach(1..5) {
print "Input: $_ ";
&Test_List($_);
}
sub Test_Shift() {
my $Test1 = shift;
print "Returns: $Test1 \n";
}
sub Test_List() {
my $Test2 = @_;
print "Returns: $Test2 \n";
}
Results
Input: 1 Returns: 1
Input: 2 Returns: 2
Input: 3 Returns: 3
Input: 4 Returns: 4
Input: 5 Returns: 5
Input: 1 Returns: 1
Input: 2 Returns: 1
Input: 3 Returns: 1
Input: 4 Returns: 1
Input: 5 Returns: 1
What am I doing wrong or misunderstanding?
UPDATE
As recommended by Ikegami, I changed the following line
#Original
my $Test2 = @_;
#New
my ($Test2) = @_;
This results in the expected output. As Ikegami has reminded me, when a list is coerced into a scalar context, the value becomes the size of the list.