in reply to Require - Module inclusion

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)

Replies are listed 'Best First'.
Re^2: Require - Module inclusion
by Anonymous Monk on Mar 16, 2007 at 08:27 UTC
Re^2: Require - Module inclusion
by kulls (Hermit) on Mar 16, 2007 at 11:42 UTC
    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

      A couple of comments:

      • I would avoid using a relative path in your @INC. That seems rather fragile. Actually, avoid using relative paths in general.
      • Insted of unshifting onto @INC, use lib, it does exactly that.
      • Generally, we name our module files with a .pm extension, but our package/class names don't include the .pm, so you'll need to address that when you call new on the package name

      Given those suggestions, try this:

      # 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;'