Well, try this out for size:sub greet { my ($name, $age) = @_; # required param $name or die "must supply name" ; # optional param, defaults to 25 $age ||= 25; print "Hello, there $name. How does it feel to be $age?\n"; }
Personally I like the Params::Validate solution. No, let me take that back... I LOVE the Params::Validate solution. It is much more definitional. This may not seem like a big win with just two parameters but when you get 8 parameters, such as in the table2() function that I recently wrote, it really begins to pay off.use Params::Validate qw(:all); sub greet { my %p = validate(@_, { name => 1, # required age => { default => 25 } }) } print "Hello, there $name. How does it feel to be $age?\n";
You have one part of your code that serves as a "firewall" making sure that everything the function requires is there and of the right type. And any optional things get their default values. It is a huge headache saver.
Not so with PV. You just validate on one more parameter and specify that it is mandatory.
sub HTML::Element::iter2 { my $tree = shift; my %p = validate( @_, { # the container look-down. defaults to ['_tag' => 'dl'] wrapper_ld => { default => ['_tag' => 'dl'] }, # the data to fill the container with. mandatory wrapper_data => 1, # the routine to preprocess the HTML::Element container # by default no preprocessing is done wrapper_proc => { default => undef }, # the routine to find the "templated" sub-elements of container # by default returns an arrayref consisting on the dt and dd tag +s item_ld => { default => sub { my $tree = shift; [ $tree->look_down('_tag' => 'dt'), $tree->look_down('_tag' => 'dd') ]; } }, # the routine to return a row of data from wrapper_data # the default routine simply shifts off a row. you might # replace this with $wrapper_data->next in some cases item_data => { default => sub { my ($wrapper_data) = @_; shift(@{$wrapper_data}) ; }}, # the routine to take the row of data and populate the item tags # the default routine takes a two-element array and fills the # dt and dd tags item_proc => { default => sub { my ($item_elems, $item_data, $row_count) = @_; $item_elems->[$_]->replace_content($item_data->[$_]) for (0,1) ; $item_elems; }}, # the routine to place the accumulated item rows back in the the # HTML::Tree. By default removes the two sample rows (dt and dd) # and replaces them with all the item rows splice => { default => sub { my ($container, @item_elems) = @_; $container->splice_content(0, 2, @item_elems); } }, # output debug info? by default, no debug => {default => 0}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Params::Validate - a powerful tool for robust subroutine design
by radiantmatrix (Parson) on Jan 23, 2006 at 17:31 UTC | |
by diotalevi (Canon) on Jan 24, 2006 at 01:02 UTC | |
by autarch (Hermit) on Jan 24, 2006 at 07:08 UTC | |
by radiantmatrix (Parson) on Jan 24, 2006 at 14:44 UTC | |
by diotalevi (Canon) on Jan 24, 2006 at 14:57 UTC | |
|
Re: Params::Validate - a powerful tool for robust subroutine design
by autarch (Hermit) on Jan 24, 2006 at 07:16 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |