in reply to Re: Re: Methods and attributes?
in thread Methods and attributes?

Why writing your own "decoder"? You could just borrow the decoder from CGI. I quickly wrote the following example to show how to decode the named parameters (nicely).
use strict; use Data::Dumper; use CGI::Util qw/rearrange/; # when called as a method __PACKAGE__->test( -option1=>'a', -option3=>'c' ); # called as a normal function test( -option1=>'a', -option3=>'c', -option2=>'b' ); # called with unnamed parameters test( 'a', 'b', 'c' ); # our little test method/function sub test { my($self,@p) = self_or_default(@_); my($option1,$option2,$option3,@other) = rearrange([qw/ OPTION1 OPTION2 OPTION3 /],@p); print "\$option1=$option1\n\$option2=$option2\n\$option3=$option3\n\ +n"; } # borrowed from CGI.pm with minor modification ;-) sub self_or_default { my $class = __PACKAGE__; # name of the class, set to main for testin +g return @_ if defined($_[0]) && (!ref($_[0])) && ($_[0] eq $class); my $Q = undef; unless (defined($_[0]) && (ref($_[0]) eq $class || UNIVERSAL::isa($_[0], $class))) { $Q = {}; bless $Q, __PACKAGE__; unshift(@_,$Q); } return wantarray ? @_ : $Q; }
And the output is just what we expected -
$option1=a $option2= $option3=c $option1=a $option2=b $option3=c $option1=a $option2=b $option3=c

Replies are listed 'Best First'.
Re: Re: Re: Re: Methods and attributes?
by stonecolddevin (Parson) on Dec 12, 2003 at 04:57 UTC
    Oh, ok, so basically just like CGI.pm's version?

    And if you're feeling lucky... come and take me home And if you feel loved If you feel lucky, if you feel loved If you feel lucky, if you feel loved You've crossed the walls - Excelled Further along through their hell All for my heart, I watch you kill You always have, you always will Now spread your wings and sail out to me....