in reply to Can @ARGV work within Perl functions or is it just for script input?

Normally, as MidLifeXis states, @_ is the argument list for a sub and @ARGV is the argument list for a script, altho' you could, if you were sufficiently perverse, do this (without affecting @ARGV in the calling context)...
sub foo { local @ARGV = @_; . . . }
Note, also, that the context of the my $argument_size = scalar(@ARGV); statement is unclear (the closing brace for either sub doesn't appear to be present), but
sub verify_function_params { my $num = $_[0]; my $function_name = $_[1]; my $input = $_[2]; my $type = $_[3]; my $argument_size = scalar(@_); print "argument_size is $argument_size\n"; . .
may well produce the results you (seem to have) expected.

Now, understanding the above, you will probably be able to see why...

sub verify_function_params { my $num = $_[0]; my $function_name = $_[1]; my $input = $_[2]; my $type = $_[3]; . .
is usually written as...
sub verify_function_params { my ($num, $function_name, $input, $type) = @_; . .

A user level that continues to overstate my experience :-))

Replies are listed 'Best First'.
Re^2: Can @ARGV work within Perl functions or is it just for script input?
by jdporter (Paladin) on Feb 09, 2009 at 20:30 UTC

    Note, of course, that code can be made to work with either @_ or @ARGV depending on whether it's inside or outside a sub: just use shift:

    my $arg1 = shift;
    :-)


    local @ARGV = @_;

    Normally there's no need to do this, of course, and is contra-idiomatic. But there is one situation when it is called for: when you are going to be calling some other function which expects its inputs in @ARGV. Example: Getopt::Long.

    use Getopt::Long; sub do_awesome_things { local @ARGV = @_; GetOptions( 'files=s' => \my @files, 'verbose!' => \my $verbose, ); warn "Going to process files @files. " . "Verbose is ".( $verbose ? 'ON' : 'OFF' ).".\n"; } do_awesome_things -v => -file => 'foo.txt', -file => 'bar.txt';
    Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.

      Even in those cases, there are usually clearer solutions...

      use Getopt::Long qw( GetOptionsFromArray ); sub do_awesome_things { GetOptionsFromArray( \@_, 'files=s' => \my @files, 'verbose!' => \my $verbose, ); warn "Going to process files @files. " . "Verbose is ".( $verbose ? 'ON' : 'OFF' ).".\n"; } do_awesome_things -v => -file => 'foo.txt', -file => 'bar.txt';

      www.jasonkohles.com
      We're not surrounded, we're in a target-rich environment!
Re^2: Can @ARGV work within Perl functions or is it just for script input?
by johngg (Canon) on Feb 09, 2009 at 20:39 UTC
    you could, if you were sufficiently perverse, do this (without affecting @ARGV in the calling context)...

    One thing to bear in mind doing that is that shift inside the subroutine will still act on @_, and will not magically transfer it's allegiance to @ARGV.

    $ perl -le ' > sub x > { > local @ARGV = @_; > print while $_ = shift; > print qq{->@ARGV<-}; > print qq{->@_<-}; > } > > x( qw{ 1 2 3 } ); > print while $_ = shift;' a b c 1 2 3 ->1 2 3<- -><- a b c $

    I hope this is of interest.

    Cheers,

    JohnGG