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

Jacinta Richardson jarich at perltraining.com.au
Sat Feb 2 19:00:54 PST 2008


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


More information about the Melbourne-pm mailing list