Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Can @ARGV work within Perl functions or is it just for script input?

by jdporter (Chancellor)
on Feb 09, 2009 at 20:30 UTC ( [id://742570]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://742570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-03-19 05:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found