Lawrence Statton wrote:

I was *JUST* lecturing my student on hash-slices Monday ...


Part of that lecture....

Passing named paramters, implemented as a hash, is a popular style.
Look at all of Perl/Tk, for example.  For systems where you might pass
dozens of parameters to a method (or subroutine), and you want each of
those values to have reasonable defaults, you really can't beat it.

An example :

#!/usr/bin/perl

use strict; use warnings;

sub frobitz {
my (%opt) = @_ ;
$opt{name} = 'blueberry' unless exists($opt{name}); $opt{color} = 'blue' unless exists($opt{color}); $opt{texture} = 'medium' unless exists($opt{texture}); $opt{price} = 30 unless exists($opt{price}); # ... do something useful }



frobitz ( name => 'banana',
color => 'yellow',
texture => 'soft',
price => 5 );

The way I've seen that done sometimes is like this:

sub frobitz {
    my %opt = (
        name => 'blueberry',
        color => 'blue',
        texture => 'medium',
        price = 30,
        @_
        );

    # ... do something useful
}

The trailing keys and values in the list override the defaults if they are present. And of course that won't work for scalars or arrays.


John -- use Perl; program fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to