* Smylers <[EMAIL PROTECTED]> [2005-04-16 14:40]:
> One nice way of dealing with this that I hadn't thought of till
> I recently spotted in Text::VimColor (though it's probably used
> in many places) is simply to bless the options hash, so you'd
> end up with something like:
> 
>   unshift @_, 'initial' if @_ % 2;
>   my %opts = (countername => $countername, @_);
>   $opts{dsn}       ||= $DSN,
>   $opts{login}     ||= $LOGIN,
>   $opts{password}  ||= $PASSWORD,
>   $opts{tablename} ||= $TABLENAME || 'counters',
>   $opts{initial}   ||= '0',
>   bless \%opts, $pkg;

    unshift @_, 'initial' if @_ % 2;
    my %param = @_;

    my %default = (
        dsn         => $DSN,
        login       => $LOGIN,
        password    => $PASSWORD,
        tablename   => $TABLENAME || 'counters',
        countername => $countername,
        initial     => '0',
    );
    my @allowed = ( keys %default, qw( and others more ) );

    my %self;
    @self{ @allowed } = @param{ @allowed };
    $self{ $_ } ||= $default{ $_ } for keys %default;
    bless \%self, $pkg;

VoilÃ: each key and each default mentioned exactly once.

Regards,
-- 
#Aristotle
*AUTOLOAD=*_=sub{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1};
&Just->another->Perl->hacker;

Reply via email to