Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by Amphiaraus (Acolyte)
on Feb 09, 2009 at 19:08 UTC ( #742531=perlquestion: print w/ replies, xml ) Need Help??
Amphiaraus has asked for the wisdom of the Perl Monks concerning the following question:

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

sub copymerge_functions_pm_init { my $input = $_[0]; my $type = $_[1]; &verify_function_params(2, "copymerge_functions_pm_init", $input, $ +type); <-- 4 arguments sub verify_function_params { my $num = $_[0]; my $function_name = $_[1]; my $input = $_[2]; my $type = $_[3]; my $argument_size = scalar(@ARGV); print "argument_size is $argument_size\n";
My output is showing --

argument_size is 0

Even though 4 arguments were given to function verify_function_params

Comment on Can @ARGV work within Perl functions or is it just for script input?
Download Code
Re: Can @ARGV work within Perl functions or is it just for script input?
by MidLifeXis (Prior) on Feb 09, 2009 at 19:13 UTC

    Within a sub, @_ is the equivalent of @ARGV in the script.

    --MidLifeXis

Re: Can @ARGV work within Perl functions or is it just for script input?
by Anonymous Monk on Feb 09, 2009 at 19:34 UTC
Re: Can @ARGV work within Perl functions or is it just for script input?
by Bloodnok (Vicar) on Feb 09, 2009 at 19:37 UTC
    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 :-))

      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!
      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

Re: Can @ARGV work within Perl functions or is it just for script input?
by Amphiaraus (Acolyte) on Feb 09, 2009 at 23:32 UTC
    Thanks all, @_ gave me the parameter checking syntax for individual functions that I needed. My code example was truncated, it only gave the opening lines of 2 functions within my Perl script.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://742531]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2014-09-23 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (208 votes), past polls