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

Alfie John alfiejohn at gmail.com
Sun Feb 3 13:34:16 PST 2008


Hey guys,

For a quick hacks, I really like Wes' method of can() since no setup
code is required and it just works. However If there are security
concerns, a dispatch table is the way to go since you can be explicit
on which functions are going to be called, provide a default when one
doesn't exist, and no private functions can ever be called.

Alfie

On Feb 3, 2008 2:00 PM, Jacinta Richardson <jarich at perltraining.com.au> wrote:
>
> Daniel Pittman wrote:
>
> >      dns mx ...         # check MX details for a domain
> >      dns ns ...         # check NS details for a domain
>
> > 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?
>
>
> Finding consensus might be a challenge... but I'd suggest using a dispatch
> table.  This seems to be a pretty common suggestion for similar problems on Perl
> Monks for example.
>
> # This is a hash of subroutine references.
> my %dispatch = (
>         dns => \&dns_function,
>         mx  => \&mx_function,
> );
>
> # Then later call the subroutine
> if(exists $dispatch{$arg}) {
>         $dispatch{$arg}->();
>         # or
>         # $dispatch{$arge}->(@args);
> } else {
>         # Unknown option
> }
>
>
> All the best,
>
>
>         J
> _______________________________________________
> 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