On Thu, Apr 16, 2009 at 11:14, Raheel Hassan <raheel.has...@gmail.com> wrote:
> Hello,
>
> I have seen some Perl scripts where some times these lines are mentioned in
> the begining of the script can any body explain these lines.
>
> *Script-1*
> use DBI;                                 # What is been called here.
snip

The use statement[1] loads a Perl module.  In this case you are loading the DBI
module.  The DBI module gives you the ability to connect to Relational
Databases.

snip
> BEGIN                                           #What operation is been
> performed by Begin
> {
> unshift �...@inc, "/home/LIBS";          #What is the role of unshift and @INC
> unshift �...@inc, "libdata/perl5/site_perl";
> }
snip

Execution of Perl programs happens in a set of phases[2]: BEGIN, UNITCHECK,
CHECK, INIT, normal code, and END.  When you write normal code it executes in
the fifth phase, but you can mark code as belonging to a given phase by
using a named block like BEGIN {} or END {}.  In this case the programmer
wants this code to run during the BEGIN phase.

The @INC array[3] holds a list of paths to look in for modules.  In this
case the programmer has installed some modules in /home/LIBS and
libdata/perl5/site_perl and wants Perl to be able to find them, so he/she
is using unshift[4] to put them at the start of the array.

The code is in a BEGIN block because use statements are also sneaky BEGIN
blocks (that is they execute in the BEGIN phase), so if the this code were
not in a BEGIN block Perl would throw an error saying that it couldn't
find the modules before his/her code could add the paths to @INC.

1. http://perldoc.perl.org/functions/use.html
2. 
http://perldoc.perl.org/perlmod.html#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END
3. http://perldoc.perl.org/perlvar.ht...@inc
4. http://perldoc.perl.org/functions/unshift.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to