CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
require won't import at runtime?

 



g.dave
New User

Nov 11, 2011, 11:52 AM

Post #1 of 5 (2418 views)
require won't import at runtime? Can't Post

Can someone provide some insight on this?

my module (MyModule.pm):


Code
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:

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


and this works

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


but this does not


Code
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?


wickedxter
User

Nov 11, 2011, 12:41 PM

Post #2 of 5 (2411 views)
Re: [g.dave] require won't import at runtime? [In reply to] Can't Post

@EXPORT_OK doesnt export by default only by request @EXPORT will export by default and wont require you to request functions

http://perldoc.perl.org/Exporter.html


(This post was edited by wickedxter on Nov 11, 2011, 12:42 PM)


g.dave
New User

Nov 11, 2011, 1:19 PM

Post #3 of 5 (2407 views)
Re: [wickedxter] require won't import at runtime? [In reply to] Can't Post

You can see from my code that I did in fact request those functions. That should be made apparent by the fact that in the first two cases the requested functions were imported successfully. Only in the last case were they not imported, and it is not clear to me why.


FishMonger
Veteran / Moderator

Nov 11, 2011, 2:45 PM

Post #4 of 5 (2401 views)
Re: [g.dave] require won't import at runtime? [In reply to] Can't Post

Do you have the strict and warnings pragmas loaded?

You should declare @EXPORT_OK as you did with @ISA.


g.dave
New User

Nov 11, 2011, 3:56 PM

Post #5 of 5 (2394 views)
Re: [FishMonger] require won't import at runtime? [In reply to] Can't Post

I've realized my answer.

This works


Code
frobnicate(); 
munge();


as does this

Code
&frobnicate; 
&munge;


Since in the last case the class methods were not imported at compile time, the compiler didn't know how to treat 'frobnicate' and 'munge' as bare words. Declaring them as functions explicitly gives the compiler the clue it needs to execute them as functions at runtime.

syntactically it is akin to a simple lexical declaration of:


Code
foo; # fails 
sub foo { print "foobar!\n" };


which does not work. reversing the order makes it work, or declaring 'foo' as a function also works:

Code
sub foo { print "foobar!\n" }; 
foo; # works!

or

Code
foo(); # works! 
sub foo { print "foobar!\n" };
&bar; # also works!
sub bar { print "barfoo!\n" };


The same principal is involved when using require and then import in a runtime manner. If I think about the timing of compile vs runtime evaluation it begins to make sense why my third example in the original post won't work.

Thanks for the input.


(This post was edited by g.dave on Nov 11, 2011, 3:58 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives