Hello
I want to update an
existing module to support an OO and procedural
interface. The module will be primarily OO, however it should still support for
two main functions as a simple procedural interface.
I looked at CGI.pm
and the way they do it is like this:
The well known param
mothod which can all be called as a procedural function or OO
function.
sub param {
my($self,@p) = self_or_default(@_);
return $self->all_parameters unless @p;
my($name,$value,@other);
my($self,@p) = self_or_default(@_);
return $self->all_parameters unless @p;
my($name,$value,@other);
etc.
}
A method to return
$self or a default object to use within the calling method
sub self_or_default {
return @_ if defined($_[0]) && (!ref($_[0])) &&($_[0] eq 'CGI');
unless (defined($_[0]) &&
(ref($_[0]) eq 'CGI' || UNIVERSAL::isa($_[0],'CGI')) # slightly optimized for common case
) {
$Q = $CGI::DefaultClass->new unless defined($Q);
unshift(@_,$Q);
}
return wantarray ? @_ : $Q;
}
return @_ if defined($_[0]) && (!ref($_[0])) &&($_[0] eq 'CGI');
unless (defined($_[0]) &&
(ref($_[0]) eq 'CGI' || UNIVERSAL::isa($_[0],'CGI')) # slightly optimized for common case
) {
$Q = $CGI::DefaultClass->new unless defined($Q);
unshift(@_,$Q);
}
return wantarray ? @_ : $Q;
}
I don't really follow
the $Q = $CGI::DefaultClass->new unless defined($Q); or DefaultClass
logic used in above.
Is there a
standard approach to making a module support both 00 and procedural interfaces?
Any ideas? I just want to create $self if there isn't one. $self should inherit
all the default properties made by new.
Dave
David Cumming
Webmaster
Corporate Affairs
Environmental Protection Agency
+61 7 3247 3274
