It's fine, except require never returns false. On error, it throws an exception. Also, \:\: would be more readable as ::.
use constant PATH => 'XX/XDC/';
my $change=PATH."raja";
require("$change.pm");
$change=~ s{/}{::}g;
$change->new();
| [reply] [d/l] [select] |
whether following block is valid
This is valid as long as it works as you have expected. perl is very intelligent enough to understand this. what is the use of doing like that ?
| [reply] |
Hi kulls,
I am getting the following error while try to run the above code. I have created raja.pm in XX/XDC directory.
use constant PATH => 'XX/XDC/';
my $change=PATH."raja";
require("$change.pm") || die "$!";
$change=~ s/\//\:\:/g;
$obj = $change->new();
print $obj;
Output:
-------
Can't locate object method "new" via package "XX::XDC::raja" (perhaps
+you forgot to load "XX::XDC::raja"?) at xyz.pl.
But the below is working fine and resulting the object reference.
unshift(@INC,"XX/XDC");
require raja;
$obj = raja->new();
print $obj;
output:
-------
raja=HASH(0x8f38d1c)
| [reply] [d/l] [select] |
package XX::XDC::raja;
| [reply] [d/l] |
Hi,
Thanks for mail.
I have tried your suggestion. it's not working for me.
I found that raja is hardcoded . Can you please try like this ?
my $pack='raja.pm';
unshift(@INC,"XX/XDC");
require "$pack";
$obj = $pack->new();
print $obj;
Let me know your comments.
-kulls
| [reply] [d/l] [select] |
# assume this is hard coded
my $package = 'raja.pm';
use lib qw ( /full/path/to/XX/DC );
# get the package name without the .pm
my ( $class ) = split /\./, $package;
# require by package name
require $package;
# call new on the class (raja)
my $obj = $class->new();
print $obj;
perl -e 'split//,q{john hurl, pest caretaker};print join q{},map{@_[$_]}q{00061314041703012005120710111907081505211622192409}=~/\d{2}/g;'
| [reply] [d/l] [select] |
require will translate a package spec to a path, but it needs to be a bareword. That can simplify your method, as long as you remember to use the string version of eval:
use constant BASE => 'XX::XDC';
my $change = BASE . '::raja';
eval "require $change";
die($@) if ($@);
$change->new();
pg
| [reply] [d/l] [select] |
use UNIVERSAL::require;
my $class = 'XX::XDC::raja';
require $class or die "can't require '$class': $@";
$class->new;
| [reply] [d/l] |