Bustamam Harun <[EMAIL PROTECTED]> writes:
>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.
>
>I have done this with methods, for instance the object AnyObj is defined
>but none of the methods are defined, so I use autoload mechanism so:
>
>use mypackage;
>
>$obj = new AnyObj();
>
>$ret = $obj->callMyMethod();
>
>In my XS, I can define an AUTOLOAD method and check the (UNO) object to
>see if the method exists and forward the call accordingly. If you want
>more details on how I do this, you can checkout the Perluno project
>(http://perluno.sourceforge.net/)
I took a look - and on the current vote for Struct I don't like
any of the options - just like a real election ;-)
my $obj = new Struct('Property', m1 => 1);
would be my choice (i.e. nearly like 1st option but without the anon array.
On the topic in hand you could auto-create classes if you were willing
to change you invocation to:
$obj = new UNO AnyObj => args;
Now UNO::new can so whatever you want.
It can also auto-create classes as UNO::AnyObj and avoid conflicts.
package UNO;
sub new
{
my ($class,$subclass,@args) = @_;
no strict 'refs';
unless (exists ${"$class::"}{$subclass})
{
@{"${class}::${subclass}::ISA"} = ...; # call class into being and set is base
class(es).
}
return "${class}::$subclass"->new(@args);
}
The above is of course possible in XS code too (possibly easier
as the clever quoting is not required and you can use the various GV
routines.)
But while we are here - beware that the
method Class
syntax is error prone
Class::->method e.g.
Uno::->new(AnyObj => args);
is much more robust.