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

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:

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("$") || die "$!"; $change=~ s/\//\:\:/g; $change->new();

Replies are listed 'Best First'.
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=~ s{/}{::}g; $change->new();
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 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 in XX/XDC directory.
    use constant PATH => 'XX/XDC/'; my $change=PATH."raja"; require("$") || 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
    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)
      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=''; unshift(@INC,"XX/XDC"); require "$pack"; $obj = $pack->new(); print $obj;

      Let me know your comments.

        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 = ''; 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();
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://605104]
Approved by GrandFather
and the daffodils sway...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2017-01-21 12:11 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (184 votes). Check out past polls.