CGI.pm does something simliar, except with a generic sub.
sub self_or_default {
return @_ if defined($_[0]) && (!ref($_[0])) &&($_[0] eq 'CGI');
unless (defined($_[0]) &&
(ref($_[0]) eq 'CGI' || UNIVERSAL::isa($_[0],'CGI')) # slightl
+y optimized for common case
) {
$Q = $CGI::DefaultClass->new unless defined($Q);
unshift(@_,$Q);
}
return wantarray ? @_ : $Q;
}
Then they use it like this:
sub url_param {
my ($self,@p) = self_or_default(@_);
my $name = shift(@p);
return undef unless exists($ENV{QUERY_STRING});
...