http://qs321.pair.com?node_id=139049

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 { # ... }