On Wed, Aug 20, 2003 at 02:09:49AM -0700, Bustamam Harun wrote:
> Is there anyway I could override how Perl resolve symbols, like for 
> instance:
> 
> use mypackage;
> 
> $obj = new AnyObj();
> 
> where AnyObj type is not defined, but in XS I want to check the type 
> database (UNO) and see if the type exists and create the new object type 
> dynamically.  Right now it will give compile error.

Richard Clamp and I did some research. This looks like a job for
UNIVERSAL::AUTOLOAD. However, the obvious approach failed:

$ perl -e 'sub UNIVERSAL::AUTOLOAD {warn $UNIVERSAL::AUTOLOAD}; $obj = AnyObj->new();'

gives

Can't locate object method "new" via package "AnyObj" (perhaps you forgot to load 
"AnyObj"?) at -e line 1.

(rather than the warning). However Richard found that if you create
a dummy package first it does work:

$ perl -e 'package AnyObj; package main; sub UNIVERSAL::AUTOLOAD {warn 
$UNIVERSAL::AUTOLOAD}; $obj = AnyObj->new();'
AnyObj::new at -e line 1.

So if you can query UNO at BEGIN time and add dummy packages for everything
you need, then this looks like a solution.

Otherwise, not sure.

It could be argued that the need for a dummy package is a bug in perl.
I don't know - UNIVERSAL::AUTOLOAD is not my strong point.

Nicholas Clark

Reply via email to