yoda54 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, Simple question I'm not to sure how to ask this but here goes. I'd like to write a script with a bunch of options using getopt, within this script I'd like to also automatically call options if some conditions are defined. How do I go about doing something like this without "no strict refs" ? Thanks!
#!/usr/bin/perl -w use strict; use warnings; use Getopt::Long; my ($test); my $a = "1"; GetOptions( "test" => \$test); \$test->() if ($a > 0); if ($test) { print "Running test.....\n"; }

20040801 Edit by davido: Changed title from '378775 : Simple question'

Replies are listed 'Best First'.
Re: Getopt configuration
by jdporter (Paladin) on Jul 30, 2004 at 18:54 UTC
    I'm not sure what you mean by "calling" options, but it seems to me that you can get what you're after by replacing
    \$test->() if ($a > 0);
    with
    $test=1 if ($a > 0);
    since $test is just a simple variable, not a subroutine.
Re: Getopt configuration
by davorg (Chancellor) on Jul 30, 2004 at 18:55 UTC

    I'm a little confused. Can you demonstrate how this program would be called. What sort of commabd line options are you expecting?

    --
    <http://www.dave.org.uk>

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

      What I would like something like: ./script --run (explicitly call sub) or ./script --someotheroptions (but checks internally for conditions and if condictions are met also run "--run" routines.) Thanks, hope this is more clear. :)
Re: Getopt configuration
by ysth (Canon) on Jul 30, 2004 at 18:58 UTC
    I can't make out what you want; can you show a sample use of that script and what it would do? Especially I don't understand what no strict refs has to do with it.
Re: Getopt configuration
by pbeckingham (Parson) on Jul 30, 2004 at 19:30 UTC

    I think you want this:

    #! /usr/bin/perl -w use strict; use Getopt::Long; my $test = 1; GetOptions ('test' => \$test); if ($test) { ... }

Re: Getopt configuration
by NetWallah (Canon) on Jul 30, 2004 at 20:03 UTC
    Another speculation on whay you may want:

    #Test getopt use strict; use warnings; use Getopt::Long; my %opt; # Main options my %oa=( # Option attributes.. this=> { type=>'i', ifset=>sub{print qq(this is set\n)} }, that=> { type=>'s' }, other=> { type=>'i' , ifset=>sub{print qq(Other is set=) .shift().q +q(\n)} } ); my @GetOpt_str; push @GetOpt_str ,"$_=$oa{$_}{type}" for keys %oa; #print "\tOptStr=@GetOpt_str;\n"; GetOptions (\%opt , @GetOpt_str); # Call all procs specified .. exists $oa{$_}{ifset} and $oa{$_}{ifset}->($opt{$_}) for keys %opt; --Run with options and output--- >perl test-getopt.pl -other=44 -this=9999 Other is set=44 this is set
    Note - the output is coming from the called sub's.

        Earth first! (We'll rob the other planets later)