Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Calling options via Sub Routines

by sunadmn (Curate)
on Sep 18, 2003 at 20:23 UTC ( #292514=perlquestion: print w/replies, xml ) Need Help??
sunadmn has asked for the wisdom of the Perl Monks concerning the following question:

Good day all I am still pretty new to perl and just really starting to get my feet wet, so I was wondering how I would correctly write a script to look for command line options to run a certain sub routine.
What I would like to be able to do is write a script that can be very interactive with the user. I would like to have the script print out a question and then depending on the answer the user gives run a certain part of the script.
print "What file do you want to edit?\n"; $file = <STDIN>; chomp($file);
Here we would do some sort of test I guess to ensure what part of the code would be ran. This is the part I guess I am most unsure about. Any advice you may offer here would be great.


Replies are listed 'Best First'.
Re: Calling options via Sub Routines
by dragonchild (Archbishop) on Sep 18, 2003 at 20:35 UTC
    There are a huge number of ways to do this. They all boil down to:
    my @values = qw(Stuff you want to pass in); print "Some question\n"; chomp(my $file = <>); if ($file eq 'A') { do_A($file, @values); } elsif ($file eq 'B') { do_B($file, @values); } elsif ... { } else { complain_that_value_sucks($file, @values); }

    Now, that's a really piss-poor thing, especially if you have more than 5-10 options. So, we can use a dispatch table. This is a preferred method of doing things.

    my %dispatch_table = ( A => \&do_A, B => \&do_B, C => \&do_C, ); my @values = qw(Stuff you want to pass in); print "Some question\n"; chomp(my $file = <>); my $sub = $dispatch_table{$file} || \&complain_that_value_sucks; $sub->($file, @values);

    Now, the dispatch table can be in a module, built on-the-fly, or whatever. What's happening here is that we're taking references to subroutines and associating them with strings. So, whenever we see a given string, we can map that to the given subroutine. This is very similar (but more powerful) to funcp's in C/C++.

    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Calling options via Sub Routines
by sri (Vicar) on Sep 18, 2003 at 20:45 UTC
    You should also consider the use of Getopt::Long over interactivity, it makes many things easier!
Re: Calling options via Sub Routines
by Roger (Parson) on Sep 18, 2003 at 23:43 UTC
    I quickly wrote some sample code below that I hope would be more helpful to set your perl programming on the right track :-D
    #!/usr/bin/perl use Getopt::Long; use Pod::Usage; # Recomend to use Pod::Usage along with Getopt::Long use strict; # Parse command line arguments and assign corresponding variables GetOptions ( 'f|fruit=s' => \( my $fruit = undef ), 'a|action=s' => \( my $action = undef ), 'g|greet' => \( my $greet = undef ), ); unless ( defined $action && defined $fruit ) { pod2usage( -exitval => 1, -output => \*STDERR ); } # do greeting print "Hello there!\n" if ($greet); # multiple fruit possible my @fruit = split /,/, $fruit; foreach (@fruit) { print "I will $action $_.\n"; # etc... } exit(0); __END__ =pod =head1 NAME =head1 SYNOPSIS [options] =head1 ARGUMENTS The -f and -a options are mandatory. The -g option is optional. =over 4 =item B<-f|--fruit [name]> Specify the fruit, multiple fruits possible, separate by comma =item B<-a|--action [name]> Specify the action to perform on fruit. =item B<-g|--greet> Print the greeting. =back =cut
    Then you can run the program with command-line options:
    perl ./ -f apple,orange -a eat -g perl ./ --fruit banana -action sell ...
Re: Calling options via Sub Routines
by davido (Archbishop) on Sep 18, 2003 at 22:11 UTC
    For the "New and just getting feet wet" folks (myself included), Getopt::Std has a pretty easy to use interface, and is flexible enough to allow multiple -a -b -c type options, as well as -abc (meaning -a -b and -c), as well as things like '-z [arg]'.

    I would provide examples, but can't possibly do so as effectively as the POD that comes with the module. See the perldocs for Getopt::Std for more info. It really is pretty simple to use and is much more robust than any "roll your own" technique could be (assuming again, that you, like I, are 'still pretty new').


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Calling options via Sub Routines
by yosefm (Friar) on Sep 19, 2003 at 10:25 UTC
    There's a nice tutorial about parsing command-line arguments, right here in the monastery. I read it (but never used it) and it looks very informative.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://292514]
Approved by dragonchild
Front-paged by dragonchild
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2017-02-28 15:55 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (405 votes). Check out past polls.