Use :
- use - Include at compile time (uses export mechianism)
- require - Include at execution time (you'll have to handle exporting task)
- do - Execute some code at execution time
| [reply] [Watch: Dir/Any] [d/l] [select] |
This isn't an answer, per se, but more of a comment. In Perl, use and require are more than #include ever was in C. The mechanisms, while resulting in something similar, do different things. If you're writing in Perl, it would behoove you to actually go ahead and take a stab at understanding what's going on and why those decisions were made. How to find this out? Read Programming Perl. Then, read Advanced Perl Programming. Ask questions here. But, please ask questions on how to understand Perl, not on how to make Perl behave like C. Yes, it can, but I feel you're missing the point. | [reply] [Watch: Dir/Any] |
or to do it exactly like the C command #include
you could use the -P switch | [reply] [Watch: Dir/Any] |
A ex-C programmer who doesn't want to create EXPORT lists might like the following variant:
- BEGIN { require "filename"; } - Include at compile time (you'll have to handle exporting task)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Notes on 2 of the above answers:
-
A Perl programmer who doesn't want to create <samp>EXPORT</samp> lists will use the simpler form
use Module ();
Also, note the subtle differences between require "Name" and require Name...
-
There's a difference between use and require (on the one hand) and do (on the other).
<samp>do</samp> essentially reads and evals the file; like any <samp>eval</samp>, it returns the value calculated. This value may be logically true, or it may not be; it doesn't matter.
<samp>require</samp> (and therefore <samp>use</samp> too) attempts to ensure some semantics are loaded. This might fail! So a required (or used) module is required (sorry) to return logical true if successful, or logical false if not. Thus the need to end those files with "1;" (or any other truth!). It's not needed if just <samp>do</samp>ing something (e.g. if loading a configuration file (unsafely)).
| [reply] [Watch: Dir/Any] [d/l] [select] |
also remember to add a 1; at the end of the included file so that the Perl compiler doesn't complain that it doesn't return a value.
# included file
# code here
1;
| [reply] [Watch: Dir/Any] [d/l] |