I just came up with the following style to write a script that is called as in foo.pl add --bar or foo.pl mod --baz etc. What do you think of this? (For perfection, rather than simply dieing on a bad parameter it should go through Pod::Usage first, but I didn't bother to add that here.) The main trick, of course, is the new main. Did I outhack myself? #!/usr/bin/perl -w
use warnings;
use strict;
print "\nfoo script\n\n";
srand time + $$ + $^T;
my $action = shift(@ARGV);
die "Usage here\n" unless $action;
my $execute = new main;
my $_action = "_$action";
die "Unknown action '$action'\n\nUsage here\n" unless can $execute $_a
+ction;
$execute->$_action();
exit;
######################################################################
+########
######################################################################
+########
use Getopt::Long;
# use Other::Modules::Here;
sub new {
my $class = shift;
my %self;
GetOptions(\%self, qw(various options here));
# do more stuff with %self here
return bless \%self, $class;
}
sub _add {
my $self = shift;
# we can see commandline parameters in %$self now
}
sub _mod {
my $self = shift;
# ditto
}
sub _del {
my $self = shift;
# again
}
sub foo {
# ...
}
sub bar {
# ...
}
-
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.
|