Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: loading modules using 'use'

by biohisham (Priest)
on Jun 30, 2010 at 12:44 UTC ( [id://847316]=note: print w/replies, xml ) Need Help??


in reply to loading modules using 'use'

use will let the module export its symbol table into the code where the module is used and @ISA indicates to perl to check the Exporter module for methods it can't find in the current module like import hence a code that uses a module can not import symbols from the module by default unless they are exported or have been marked as OK for exportation. This is mentioned in perldoc use and perldoc require.

@EXPORT array allows you to add other symbols to be exported.

Replying to your Re^2: loading modules using 'use' comment

where I introduce a BEGIN block. How does that help ?
Help in what sense? Well, The Begin block is run even before the rest of the module file is parsed by perl however, can you clarify a bit on what you mean by 'help'?...


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

Replies are listed 'Best First'.
Re^2: loading modules using 'use'
by angshuman (Novice) on Jun 30, 2010 at 13:44 UTC
    By 'help', what I wanted to find was a specific situation where putting the require-an-all statements in a BEGIN block in a package module would come into effect, as in normal conditions both without BEGIN and with BEGIN performs exactly the same.

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found