Quote:
># This works:
>package Foo;
>use MyPackage::MyClass;
>my $obj = MyPackage::MyClass->new($myargs);
># This dies with "Can't find method new":
>package Foo;
>my $class = "MyPackage::MyClass";
>my $obj = eval $class . '->new($myargs)';
Has the interpreter had a chance to reaf the MyPackage::MyClass module
anywhere before this? Just invoking a method won't cause perl to go and
load the class. MyPackage::MyClass::new must have been presented to perl
for that call to succeed (unless you're into advanced trickery to fake up
the sub on demand). In the de{*filter*}:
DB<1> $class = 'CGI'
DB<2> print "worked" if $o = eval $class . '->new({})';
DB<3> use CGI
DB<4> print "worked" if $o = eval $class . '->new({})';
worked
At step 2 nothing had caused perl to read the CGI module, once read the
statement worked.
You are aware that $myargs will not interpolate inside single quotes,
so you may have a problem there. You can say
DB<5> print "worked" if $o = $class->new({});
worked
DB<6> print $o
CGI=HASH(0x832dd54)
Quote:
># this dies with can't find MyPackage::MyClass;
>package Foo;
>my $class = "MyPackage::MyClass";
>require $class;
>What am I doing wrong? I must have screwed with this for two hours today!
>I've done this type of thing before without problems but it does not seem to
>be working this time... Grr.
Did you spend some time reading the manual, which should be installed with
a perl installation? On my system
perldoc -tf require
includes this:
require VERSION
require EXPR
[...]
If EXPR is a bareword, the require assumes a ".pm" extension and
replaces "::" with "/" in the filename for you, to make it easy
to load standard modules. This form of loading of modules does
not risk altering your namespace.
In other words, if you try this:
require Foo::Bar; # a splendid bareword
The require function will actually look for the "Foo/Bar.pm"
But if you try this:
$class = 'Foo::Bar';
require $class; # $class is not a bareword
#or
require "Foo::Bar"; # not a bareword because of the ""
The require function will look for the "Foo::Bar" file in the
In this case you can do:
eval "require $class";
For a yet-more-powerful import facility, see the use entry
elsewhere in this document and the perlmod manpage.
It's worth spending some time reading the docs, and getting a feel for
what's happening. Using the de{*filter*} and examining things like %INC, and
understanding the difference between perl's compile and run phases will
pay off handsomely.
Hope this helps,
Mike
--
http://www.*-*-*.com/ ~mike/ |
GPG PGP Key 1024D/059913DA | Fingerprint 0570 71CD 6790 7C28 3D60