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
...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|