in reply to non-option argument question

G'day dr_jkl,

Probably a typo: I'll assume my $opt should be my %opt.

If %opt is in scope, you can modify it whenever you want. So, the question seems to boil down to how to get the value to assign to $opt{input}.

The Getopt::Long documentation has a Mixing command line option with other arguments section. Is this of any help?

An example of what you want to type on the command line would be useful.

I'm guessing your code looks something like:

my %opt = ( ... ); GetOptions( ... ); # get value for $opt{input} here if ($opt{input} eq 'something') { # do something here } else { # do something else here }

Filling in the blanks (or correcting my guess) would probably lead to a better answer.

-- Ken

Replies are listed 'Best First'.
Re^2: non-option argument question
by dr_jkl (Acolyte) on Sep 12, 2012 at 06:16 UTC

    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.

      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!