Consider the following basic Perl modulino:
#!/usr/bin/perl -l
package Toto;
__PACKAGE__->run(@ARGV) unless caller();
sub run
{
print "@ARGV";
print "@_";
}
1;
If I run it on the command line, I get:
$ ./Toto.pm 1 2 3
1 2 3
Toto 1 2 3
If I call it from a test:
$ perl -MToto -le 'Toto::run(1,2,3)'
#first line blank - no ARGV set
1 2 3
In other words, the contents of @_ inside run() changes depending on how the function is called.
Can you explain what is going on?
__PACKAGE__->run(@ARGV)
is equivalent to
Toto->run(1,2,3)
This is a class method call. Method calls pass the invocant (the value to which the LHS of the ->
evaluated) as the first argument. This differs from
Toto::run(1,2,3)
which is a simple sub call. The following will call run
as a sub:
run(@ARGV) unless caller();