Perl Module Basics
You can use modules without exporting symbols by using require instead of use:
#!/usr/bin/perl
require File::Find;
File::Find::find(sub { print "$File::Find::name\n" if s > 1_024_000; }, . );
As a result, the reference to find must be prefixed with a full package name and
written as File::Find::find.
Another difference between the two is that use happens during compile time,
whereas require happens at runtime. Perl runs a program in two phases first, the
program and all modules used by the program are compiled into an internal byte
code format. This is known as compile time. Next, the byte code is executed and
the program actually runs. Perl programs can actually go back and forth between
runtime and compile time using two mechanisms: BEGIN and eval.
A BEGIN block is a way of getting a bit of runtime during compile time. When
Perl encounters a BEGIN block, it executes the code found inside the BEGIN block as
soon as it has been compiled, before any code that comes later in the program. For
example, this line will print even if there's a compilation error later in the script:
BEGIN { print "Hello! I m running in the middle of compiling.\n" }
An eval with a string argument is a way of getting a bit of compile time during
runtime. For example, this code will be compiled and run after the program is in
runtime, which allows the code to be built during runtime:
eval "print Hello! I m compiling in the middle of running.\n";
Since use is just a way of doing a require operation and an import operation at
compile time, use can be defined in terms of require using BEGIN:
BEGIN { require File::Find; import File::Find; }
And require can be defined in terms of use with eval:
eval "use File::Find ();";
The pair of parenthesis after File::Find tells use not to import any symbols, which
emulates how require works.
23
23
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved