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 {
# ...
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.