Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Use a module only if it is in use

by vitoco (Friar)
on Sep 02, 2009 at 14:22 UTC ( #792925=perlquestion: print w/ replies, xml ) Need Help??
vitoco has asked for the wisdom of the Perl Monks concerning the following question:

I want to use a module M from another module X, only if the calling program P currently uses it.

I have the following test case that works, but X asumes that P uses M:

P.pl:

#!perl use strict; use warnings; use M; use X; hello("world"); bye();

M.pm:

package M; require Exporter; @ISA = (Exporter); @EXPORT = qw(hello); use warnings; use strict; sub hello { print "Hello @_!\n"; }

X.pm:

package X; require Exporter; @ISA = (Exporter); @EXPORT = qw(bye); use warnings; use strict; sub bye { M::hello("again"); # if exists M:hello(); print "That's all, folks!\n"; }

What should be checked in X to recognize that M is loaded and available? If it's not, do nothing!

BTW, in real life, M could be any CPAN's module.

Comment on Use a module only if it is in use
Select or Download Code
Re: Use module only if it is in use
by ikegami (Pope) on Sep 02, 2009 at 14:27 UTC

    Why?

    Check %INC to see if a module was loaded.

    Update: Fixed markup

      It was that simple!!!

      M::hello("again") if $INC{"M.pm"};

      Is it safe to asume that double colons in module names get always translated into slashes ("::" -> "/") instead of backslashes or whatever is used by the OS for subdirs?

        Yes, but again, why? I can't think of a scenario where this would possibly be useful. It feels like you're testing for a side-effect of the problem, and not for the problem itself.
        Is it safe to asume that double colons in module names get always translated into slashes ("::" -> "/") instead of backslashes or whatever is used by the OS for subdirs?
        No, it's not safe to assume that. And be lucky. Your code wouldn't work if Perl changed the '::' to a '/', because that it would try to divide M by hello("again"), which most likely results in an error.

        Perl maps '::' to the appropriate directory separator when it's used as a bareword argument to use and require. But that isn't always.

Re: Use a module only if it is in use
by almut (Canon) on Sep 02, 2009 at 14:45 UTC
    M::hello("again"); # if exists M:hello();

    Not exactly sure what you want to achieve... but maybe you just want

    M::hello("again") if defined &M::hello;

      The & was what I forgot in my tests!!!

      M::hello("again") if exists &M::hello;

      also works. Thank you!

        exists returns true if the sub is declared, whether it's been defined or not.

        defined returns true if the sub is defined.

        sub foo; # exists but not defined sub bar {} # exists and defined

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-09-01 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls