Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

import doesn't work at runtime?

by gdave (Novice)
on Nov 11, 2011 at 21:36 UTC ( [id://937673]=perlquestion: print w/replies, xml ) Need Help??

gdave has asked for the wisdom of the Perl Monks concerning the following question:

so it seems I can only import() from MyModule at compile time ('use' or BEGIN') but not at runtime. Here's my test case:

my module (MyModule.pm):

package MyModule; use Exporter(); our @ISA = qw(Exporter); @EXPORT_OK = qw(munge frobnicate); sub frobnicate { print "frobnicated!\n"; } sub munge { print "munged!\n"; }

When I call it from a script, this works (meaning I see the output from the print statements):

use MyModule qw(frobnicate munge); frobnicate; munge;

output is:

frobnicated! munged!

and this works (same output)

BEGIN { require MyModule; MyModule->import( qw(frobnicate munge) ); } frobnicate; munge;

but this does not (no output)

require MyModule; MyModule->import( qw(frobnicate munge) ); frobnicate; munge;

This seems to me to imply that I can only import at compile time, not at run-time.

Am I missing something?

Replies are listed 'Best First'.
Re: import doesn't work at runtime?
by johnny_carlos (Scribe) on Nov 11, 2011 at 21:56 UTC
    It works for me when adding parens to the subroutines:
    require MyModule; MyModule->import( qw(frobnicate munge) ); frobnicate(); munge();
      aha! &frobnicate and &munge also do the trick. I think I get it now, thanks.

        Yes, part of the consequence of having & is to identify a word as being a function name. However, there are other consequences. If you don't know what those consequences are, avoid using &.

        As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: import doesn't work at runtime?
by CountZero (Bishop) on Nov 11, 2011 at 22:22 UTC
    In perldoc perlsub we read:
    A subroutine may be called using an explicit & prefix. The & is optional in modern Perl, as are parentheses if the subroutine has been predeclared.

    So, unless you have predeclared your subs, you cannot call a sub without the parentheses. A non-predeclared sub without parentheses is just a bareword.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: import doesn't work at runtime?
by bluescreen (Friar) on Nov 11, 2011 at 22:03 UTC

    if you turn on use strict; and use warnings; you will realize that at compile time the interpreter complains that frobnicate and munge are not defined, coz they haven't been exported yet and the compiler doesn't know what they mean

Re: import doesn't work at runtime?
by Perlbotics (Archbishop) on Nov 11, 2011 at 22:09 UTC

    You're missing some parenthesis and some strictures to explain the problem. Without parens:

    perl -e 'use strict; use warnings; require MyModule; MyModule->import( +qw(frobnicate munge)); frobnicate; munge; ' Bareword "frobnicate" not allowed while "strict subs" in use at -e lin +e 1. Bareword "munge" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.
    With:
    perl -e 'use strict; use warnings; require MyModule; MyModule->import( +qw(frobnicate munge)); frobnicate(); munge(); ' frobnicated! munged!
    With parens, Perl knows at compile time that frobnicate()/munge() are subroutines and that Perl can deal with them later at runtime. So, the following also works, but changes the semantics a bit (first argument is "MyModule") - Perl still detects the subroutine invocation (w/o parens):
    perl -e 'use strict; use warnings; require MyModule; MyModule->import( +qw(frobnicate munge)); MyModule->frobnicate; MyModule->munge; ' frobnicated! munged!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://937673]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found