cees has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to write a method that can accept as input a CGI.pm like object. Instead of using 'isa' to find out if it is a CGI.pm object, I would like to check if the module provides the methods I am looking for using UNIVERSAL::can. This way other modules like CGI::Simple or Apache::Request can also be used.
What I am wondering is, what is the cleanest way to deal with the fact that CGI.pm autoloads methods on demand, hiding them from UNIVERSAL::can. The only thing I can come up with is actually calling the method in question in an eval {} first and then checking it with UNIVERSAL::can...
use CGI (); my $q = CGI->new; print "can cookie: ", UNIVERSAL::can($q, 'cookie') ? 'Yes' : 'No', $/; eval { $q->cookie }; print "can cookie: ", UNIVERSAL::can($q, 'cookie') ? 'Yes' : 'No', $/;
can cookie -> No can cookie -> Yes
But I was hoping there was a cleaner way to do this without actually having to call the method.
CGI::Simple doesn't seem to suffer from this since it uses AutoLoader, which will create stubs for the autoloaded subroutines, but CGI.pm uses it's own method...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: UNIVERSAL::can and autoloaded methods
by tilly (Archbishop) on Dec 01, 2003 at 15:48 UTC | |
by cees (Curate) on Dec 01, 2003 at 19:12 UTC | |
|
Re: UNIVERSAL::can and autoloaded methods
by scrottie (Scribe) on Dec 02, 2003 at 10:01 UTC |