Aristotle has asked for the wisdom of the Perl Monks concerning the following question:
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 { # ... }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Command line tool coding style?
by chromatic (Archbishop) on Jan 16, 2002 at 02:14 UTC | |
by Aristotle (Chancellor) on Jan 16, 2002 at 05:34 UTC | |
by chromatic (Archbishop) on Jan 16, 2002 at 06:36 UTC | |
Re: Command line tool coding style?
by perrin (Chancellor) on Jan 16, 2002 at 02:46 UTC | |
by Aristotle (Chancellor) on Jan 16, 2002 at 05:34 UTC | |
by perrin (Chancellor) on Jan 16, 2002 at 07:43 UTC | |
by Aristotle (Chancellor) on Jan 16, 2002 at 17:40 UTC | |
by perrin (Chancellor) on Jan 16, 2002 at 23:10 UTC | |
| |
Re: Command line tool coding style?
by muba (Priest) on Oct 23, 2004 at 20:56 UTC | |
by Aristotle (Chancellor) on Oct 24, 2004 at 03:55 UTC |
Back to
Seekers of Perl Wisdom