in reply to Methods and attributes?

Perhaps you should have a look at how CGI.pm does it, ie., read the source code, learn from the master...

A particularly interesting function is rearrange in CGI::Util that CGI uses to decode the parameters.
# Smart rearrangement of parameters to allow named parameter # calling. We do the rearangement if: # the first parameter begins with a - sub rearrange { ....

Replies are listed 'Best First'.
Re: Re: Methods and attributes?
by stonecolddevin (Parson) on Dec 11, 2003 at 05:59 UTC
    Ah yes, see, that is where I started. I've looked through somewhat thoroughly with not much luck, so I bet I was missing one of the "decoding" subs.

    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....
      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
        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....