Why do
my $i=0;
my @arr=();
sub readall {
foreach (@_) {
$arr[$i] = shift @_;
$i++;
}
}
readall(1, 2, 3, 4, 5);
print "@arr"
and
my $i=0;
my @arr=();
sub readall {
foreach (@_) {
$arr[$i] = shift @_;
print $arr[$i];
$i++;
}
}
readall(1, 2, 3, 4, 5);
print only three of the arguments to readall?
Why does this function, which seems like it should behave the same, process all five arguments?
sub readall {
foreach (@_) {
print $_;
}
}
readall(1, 2, 3, 4, 5);
This also reads all five (but does operate on a different principle):
my @arr=();
sub readall {
push(@arr, @_);
}
readall(1, 2, 3, 4, 5);
print "@arr"
Every time you shift your array, it gets shorter… So you are not operating on the whole array, and it will stop early. You can see this by adding a line to your code:
I assume you can figure it out from here.