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
|