Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by jdporter (Canon)
on Feb 09, 2009 at 20:30 UTC ( #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.


Comment on Re^2: Can @ARGV work within Perl functions or is it just for script input?
Select or Download Code
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
Node Status?
node history
Node Type: note [id://742570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-07-25 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (173 votes), past polls