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

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.

Replies are listed 'Best First'.
Re^3: Can @ARGV work within Perl functions or is it just for script input?
by jasonk (Parson) on Feb 10, 2009 at 16:46 UTC

    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!