Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Calling options via Sub Routines

by Roger (Parson)
on Sep 18, 2003 at 23:43 UTC ( [id://292541]=note: print w/replies, xml ) Need Help??


in reply to Calling options via Sub Routines

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 fruit.pl =head1 SYNOPSIS fruit.pl [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 ./fruit.pl -f apple,orange -a eat -g perl ./fruit.pl --fruit banana -action sell ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://292541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-20 12:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found