Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
No such thing as a small change
 
PerlMonks  

Require - Module inclusion

by kulls (Hermit)
on Mar 16, 2007 at 05:46 UTC ( #605104=perlquestion: print w/ replies, xml ) Need Help??
kulls has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,
Can anyone please correct me , whether following block is valid,acceptable.I have tested the real snippets which is working fine .
Any alternate is there ??
use constant PATH => 'XX/XDC/'; my $change=PATH."raja"; require("$change.pm") || die "$!"; $change=~ s/\//\:\:/g; $change->new();

Comment on Require - Module inclusion
Download Code
Re: Require - Module inclusion
by qsl (Scribe) on Mar 16, 2007 at 06:28 UTC
    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 ?
Re: Require - Module inclusion
by ikegami (Pope) on Mar 16, 2007 at 07:02 UTC

    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();
Re: Require - Module inclusion
by siva kumar (Pilgrim) on Mar 16, 2007 at 07:06 UTC
    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)
      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;'
Re: Require - Module inclusion
by pgor (Beadle) on Mar 16, 2007 at 20:59 UTC
    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
Re: Require - Module inclusion
by valdez (Monsignor) on Mar 18, 2007 at 09:54 UTC

    I would use UNIVERSAL::require:

    use UNIVERSAL::require; my $class = 'XX::XDC::raja'; require $class or die "can't require '$class': $@"; $class->new;

    Ciao, Valerio

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://605104]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-04-23 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (551 votes), past polls