Dual-license your content for inclusion in The Perl 5 Wiki using this HOWTO, or join us for a chat on irc.freenode.net#PerlNet.
User:Blm/UseRequireEvalDo
From PerlNet
< User:Blm
Contents |
[edit]
require
require VERSION
demands that the Perl binary that the source code is run under is at least the version specified or greater
require BARE::PACKAGE::NAME
- looks for the package in the paths indicated in @INC.
- does not introduce any of the symbols from the package into the current namepace.
require "PACKAGE::NAME";
or
$class = 'PACKAGE::NAME"; require $class;
looks for PACKAGE::NAME in the paths in @INC and complains at compile time.
[edit]
use
use BARE::PACKAGE::NAME
- does similar to require but imports symbols
indeed:
use MODULE list;
is the same as
BEGIN { require MODULE; import MODULE list; }
or
require MODULE
is similar to
use MODULE ();
- The empty list at the end stops symbols from being imported.
- if the package cannot be found error generated at compile time.
- As I understand it Perl looks for all the use statements at compile time and brings them in then whereas require happens at runtime
- You can also specify the lowest version of a module that can be used.
use MODULE version;
[edit]
eval()
eval BLOCK
- BLOCK is a {}
- trap runtime errors that would prove fatal.
- If a trappable error occurs then eval returns undef.
- syntax checked at compile time
eval expr
- code is instead parsed at runtime.
- syntax errors are caught and appear in $@.
- executed in the context of the program that is executing the eval expression so you can see lexical variables (cf do())
[edit]
do()
do FILE;
- FILE is a string that is a filename
- do causes the execution of the script.
- HOWEVER the code in the do() does not have access to the lexicals that are in the Perl code executing the do statement.
- what is returned from the do function is the last thing that is evaluated.
- See User:PJF post about using do() for configuration.

