So shouldn't @_ say 0?
Arguments are passed by reference but in your case you end up with an array of one element, that one element being a reference to the non-existent element of @ARGV. So, @_ has one element even though that element is a reference to a non-existent element of @ARGV.
Consider the following example:
use strict;
use warnings;
use Devel::Peek;
Dump(\@ARGV);
mysub($ARGV[0], $ARGV[1]);
mysub(@ARGV);
exit(0);
sub mysub {
print "mysub\n";
Dump(\@_);
}
__END__
SV = RV(0x83591fc) at 0x83591f0
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x83693c0
SV = PVAV(0x835a228) at 0x83693c0
REFCNT = 2
FLAGS = ()
ARRAY = 0x0
FILL = -1
MAX = -1
ARYLEN = 0x0
FLAGS = (REAL)
mysub
SV = RV(0x83590dc) at 0x83590d0
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x8376420
SV = PVAV(0x835a28c) at 0x8376420
REFCNT = 3
FLAGS = ()
ARRAY = 0x838a418
FILL = 1
MAX = 3
ARYLEN = 0x0
FLAGS = (REAL)
Elt No. 0
SV = PVLV(0x83871d4) at 0x83591f0
REFCNT = 2
FLAGS = (GMG,SMG)
IV = 0
NV = 0
PV = 0
MAGIC = 0x839e158
MG_VIRTUAL = &PL_vtbl_defelem
MG_TYPE = PERL_MAGIC_defelem(y)
TYPE = y
TARGOFF = 0
TARGLEN = 1
TARG = 0x83693c0
SV = PVAV(0x835a228) at 0x83693c0
REFCNT = 3
FLAGS = ()
ARRAY = 0x0
FILL = -1
MAX = -1
ARYLEN = 0x0
FLAGS = (REAL)
Elt No. 1
SV = PVLV(0x8387200) at 0x8359340
REFCNT = 2
FLAGS = (GMG,SMG)
IV = 0
NV = 0
PV = 0
MAGIC = 0x839eee8
MG_VIRTUAL = &PL_vtbl_defelem
MG_TYPE = PERL_MAGIC_defelem(y)
TYPE = y
TARGOFF = 1
TARGLEN = 1
TARG = 0x83693c0
SV = PVAV(0x835a228) at 0x83693c0
REFCNT = 3
FLAGS = ()
ARRAY = 0x0
FILL = -1
MAX = -1
ARYLEN = 0x0
FLAGS = (REAL)
mysub
SV = RV(0x83591fc) at 0x83591f0
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x83696f0
SV = PVAV(0x835a23c) at 0x83696f0
REFCNT = 3
FLAGS = ()
ARRAY = 0x83a5f30
FILL = -1
MAX = 3
ARYLEN = 0x0
FLAGS = (REAL)
Note in the above that the array @ARGV is located at 0x83693c0 ( SV = PVAV(0x835a228) at 0x83693c0 ). It has no elements (ARYLEN = 0x0).
In the first call to mysub() there are two arguments: elements 0 and 1 or @ARGV. But the array @_ is not a simple array of values (see SV = PVLV(0x83871d4) at 0x83591f0 and following). The array has two elements and each of these have MAGIC and a reference to the @ARGV array. The first element of @_ has TARGOFF 0 and TARGLEN 1. These are offest and length into the array SV = PVAV(0x835a228) at 0x83693c0, which is @ARGV, which still has no elements (ARYLEN = 0x0). The second element is similar to the first but TARGOFF is 1, for element 1 of @ARGV instead of element 0.
So, when you access $_[0], through the magic of the @_ array you get the value of $ARGV[0] and when you access $_[1] you get the value of $ARGV[1], but when you evaluate @_ in a scalar context you get 2 (the size of the @_ array) rather than 0 (the size of the @ARGV array). In this example the @_ array really does have two elements. In your script it really did have one element.
The second call to mysub() has @ARGV as its argument list. This array is flattened and, as it has no elements in it, it is flattened into an empty list. Thus in the second call @_ has no elements.
You might wonder, if perl passes arguments by reference, why, in the second call to mysub(), @_ isn't a reference to @ARGV (i.e. accessing @_ is the same as accessing @ARGV). I wasn't party to the decisions about how this works, so I can't tell you why. I can only tell you that in this case @_ is not the same as @ARGV, other than in the sense that they are both empty.
|