Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: loading modules using 'use'

by Taulmarill (Deacon)
on Jul 01, 2010 at 12:04 UTC ( #847500=note: print w/ replies, xml ) Need Help??


in reply to Re^2: loading modules using 'use'
in thread loading modules using 'use'

I'm unsure if i got exactly what you are asking, but let me give you some examples. Let's start with a fairly common thing:

use CGI qw/:standard/; print header;

So far, so simple. Now, what perl is doing here, can also be written like this:

BEGIN { require CGI; CGI->import( ':standard' ); } print header;

You'll notice, this gives you the same output as the first example. Now let's see what happens, when we don't use the BEGIN block.

require CGI; CGI->import( ':standard' ); print header;

The header is not printed out, if we run this. Activating the warnings gives a few hints to what is going on.

Unquoted string "header" may clash with future reserved word at test.p +l line 12. Name "main::header" used only once: possible typo at test.pl line 12. print() on unopened filehandle header at test.pl line 12.

Now what happened here?!? perl doesn't know how to handle header anymore, because at the compile time of the program, the subroutine is not yet known to perl. It gets imported at runtime, since require doesn't do anything at compile time, and perl thinks, handler is supposed to be a file handle.

Hope this cleared some thing up for you


Comment on Re^3: loading modules using 'use'
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-09-30 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (381 votes), past polls