[Melbourne-pm] Perl best practices: script actions based on acommand line argument

Wesley Moore wjmoore at gmail.com
Thu Jan 31 22:22:31 PST 2008


Pretty sure some might consider this ugly but it works and doesn't get
bigger the more functions you add:

#!/opt/local/bin/perl

use strict;
use warnings;

my $cmd = shift;
die 'Usage is....' unless($cmd);

my $value;
if(__PACKAGE__->can($cmd)) {
    $value = __PACKAGE__->$cmd(@ARGV);
}
else {
    die "$cmd is not a valid command";
}

exit $value;

sub func_one {
    print "one\n";
    return 0;
}

sub func_two {
    print "two\n";
    return 0;
}

sub func_three {
    print "three\n";
    return 0;
}

Sample execution:

% perl dispatch.pl
Usage is.... at dispatch.pl line 7.

% perl dispatch.pl fun_one
fun_one is not a valid command at dispatch.pl line 14.

% perl dispatch.pl func_one
one

% perl dispatch.pl func_three
three

Wes

On Feb 1, 2008 5:18 PM, Leigh Sharpe <lsharpe at pacificwireless.com.au> wrote:
>
>
> What about:
>
> My $mode=shift;
> $mode eq 'dns' and dns_function();
> $mode eq 'mx' and mx_function();
> # etc.
> # etc.
>
> It's a little less ugly than the cascading if..else's.
>
>
>
>
>
>
>
> -----Original Message-----
> From: melbourne-pm-bounces+lsharpe=pacificwireless.com.au at pm.org
> [mailto:melbourne-pm-bounces+lsharpe=pacificwireless.com.au at pm.org] On
> Behalf Of Daniel Pittman
> Sent: Friday, 1 February 2008 4:42 PM
> To: melbourne-pm at pm.org
> Subject: [Melbourne-pm] Perl best practices: script actions based on
> acommand line argument
>
> G'day.
>
> I semi-regularly end up writing small scripts that do little jobs for
> myself, such as performing various DNS checks, interfacing to billing
> systems and the like.
>
> Generally these end up as a single script that handles half a dozen
> closely related but mostly independent functions, such as:
>
>     dns mx ...         # check MX details for a domain
>     dns ns ...         # check NS details for a domain
>
> I typically implement this as a bunch of supporting code and libraries,
> and a command wrapper that read the first command line argument and
> dispatches to a function based on it.
>
> This being Perl there are a lot of ways to do that, all of which are
> a bit ugly (in my opinion), such as a cascade of if ($blah eq 'foo')
> statements or a hash full of function refs...
>
>
> So, what is the general consensus on the best way to do this -- what is
> the nicest way to dispatch to the various command handlers based on a
> command line argument?
>
>
> Regards,
>        Daniel
> --
> Daniel Pittman <daniel at cybersource.com.au>           Phone: 03 9428 6922
> 1/130-132 Stawell St, Richmond              Web: http://www.cyber.com.au
> Cybersource: Australia's Leading Linux and Open Source Solutions Company
> _______________________________________________
>
>
>
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
>


More information about the Melbourne-pm mailing list