in reply to Re: non-option argument question
in thread non-option argument question

Yes! It should be %opt, and I've corrected it above. Sorry, it's somewhat late here. :)

I would like to be able to do something like:

 foo.pl ex-post-facto or  foo.pl -v ex-post-facto where ex-post-facto is  $opt{input} and then I can decide what to do with the input based on what switch(es) were passed.

Replies are listed 'Best First'.
Re^3: non-option argument question
by kcott (Archbishop) on Sep 12, 2012 at 08:36 UTC

    Beyond the assignment to $opt{input}, your requirement seems to be for the normal operaton of Getopt::Long.

    Consider this code:

    #!/usr/bin/env perl use 5.010; use strict; use warnings; use Getopt::Long; my %opt; GetOptions(\%opt, 'v'); say 'Before assigning $opt{input}:'; if ($opt{v}) { say $ARGV[0], ' (verbose)'; } else { say $ARGV[0], ' (quiet)'; } $opt{input} = $ARGV[0]; say 'After assigning $opt{input}:'; if ($opt{v}) { say $opt{input}, ' (verbose)'; } else { say $opt{input}, ' (quiet)'; }

    Using your two command line examples, this produces:

    $ pm_getopt_arg.pl ex-post-facto Before assigning $opt{input}: ex-post-facto (quiet) After assigning $opt{input}: ex-post-facto (quiet)
    $ pm_getopt_arg.pl -v ex-post-facto Before assigning $opt{input}: ex-post-facto (verbose) After assigning $opt{input}: ex-post-facto (verbose)

    -- Ken

      I suppose this is what I get for trying to write perl at 2am. Thank you very much!