Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: When C<use Module;> *not* the same as C<require Module; import Module;>?

by etcshadow (Priest)
on Jan 09, 2005 at 02:12 UTC ( #420620=note: print w/replies, xml ) Need Help??

in reply to When C<use Module;> *not* the same as C<require Module; import Module;>?

You've actually hit on the reason why use is automatically treated as a compile-time directive (i.e., wrapped in a BEGIN{...} block): it alters the parser's syntactic interpretation of code. Specifically, there are places where the same piece of code can have ambiguous syntactic interpretations, depending on whether or not certain symbols are known to be subroutines.

For example, take:

print foo;
If foo is already known by the perl parser to be a subroutine, it will interpret this as meaning:
However, if the interpreter does not know that foo is supposed to be a subroutine, then it will interpret foo to be the name of a file-handle (the indirect argument to print). This is all happening at compile-time (well, parse-time, really... but parse and compile happen together), so it doesn't matter whether or not foo is a subroutine at runtime... because the parser will have already decided that it was being used as the name of a filehandle, at runtime.
------------ :Wq Not an editor command: Wq

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2019-10-14 07:27 GMT
Find Nodes?
    Voting Booth?