in reply to Using a 'getopt' in module code

Getopt works on @ARGV, so you can't use it to process function arguments. Well, not without doing @ARGV = @_ which might well break other stuff.

In situations like this, most people would probably pass the function arguments as a hash.

my_func(foo => 1, bar => 2); sub my_func { my %args = @_; if ($args{foo}) { ... } # etc... }
--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Using a 'getopt' in module code
by calin (Deacon) on Dec 04, 2006 at 20:55 UTC
    Getopt works on @ARGV, so you can't use it to process function arguments. Well, not without doing @ARGV = @_ which might well break other stuff.

    You can try something like this:

    #!/usr/bin/perl use strict; use warnings; use Getopt::Std; use Data::Dumper; sub getopt_fun { my %args; { local @ARGV = @_; getopt 't', \%args; } print Dumper \%args; } getopt_fun (-t => 'foo');

    Edit: narrowed the scope of the local.