in reply to UNIVERSAL::can and autoloaded methods

By coincidence I was just discussing something like this over the weekend at Re: Re: Re: Re: Symbol table globbing from object refs. From that, one solution is to write a custom can method for CGI that you can then submit to the maintainer.

Here is a cheap alternate solution that involves poking into internals:

sub load_in_cgi { my $func = shift; eval { local $CGI::AUTOLOAD = $func; CGI::_compile(); }; }
This is basically what you have already but it avoids actually calling the method.

The third approach is to use isa and fallback on can. That is, assume that any CGI object will do what you want, but be willing to accept just having the methods. That avoids having to poke around in internals.

Replies are listed 'Best First'.
Re: Re: UNIVERSAL::can and autoloaded methods
by cees (Curate) on Dec 01, 2003 at 19:12 UTC

    Thanks for the example! I think I will go with the third approach. I think CGI.pm is most likely the only module I will face with this problem, so I will put in a specific check for it. I was hoping to have it completely generic, but this still looks clean enough for me.

    if ( UNIVERSAL::isa($q, 'CGI') || (UNIVERSAL::can($q, 'param') && UNIVERSAL::can($q, 'cookie') ) { ... }

    Cheers,

    - Cees