Home arrow Perl Programming arrow Page 6 - Understanding Perl's Special Variables

The Right Path - Perl

Perl comes with a whole bunch of cryptically-named built-invariables, which clever Perl programmers exploit to reduce the number oflines of code in their scripts. This article examines some of the morecommonly-used special variable in Perl, with examples and illustrations ofhow they may be used.

TABLE OF CONTENTS:
  1. Understanding Perl's Special Variables
  2. In Default
  3. Input...
  4. ...And Output
  5. Getting Into An Argument
  6. The Right Path
  7. To Err Is Human
  8. A Question Of Ownership
  9. Rank And File
  10. Calling For A Translator
  11. End Zone
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 38
July 10, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

A number of other special array variables also exist, in addition to @ARGV.
One of the more commonly-used ones is the @INC variable, which sets up the
"include paths" that Perl will look in when it encounters a call to
require() or use(). This is analogous to the UNIX $PATH variable, which
sets up default search paths for system binaries.

Let's take a look at this variable with the Data::Dumper module, used to
"stringify" Perl data structure.


#!/usr/bin/perl

# use data dumper
use Data::Dumper;

# examine data structure
print Dumper @INC;

Here's what it looks like:


$VAR1 = '/usr/lib/perl5/5.8.0/i386-linux-thread-multi';
$VAR2 = '/usr/lib/perl5/5.8.0';
$VAR3 = '/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi';
$VAR4 = '/usr/lib/perl5/site_perl/5.8.0';
$VAR5 = '/usr/lib/perl5/site_perl';
$VAR6 = '/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi';
$VAR7 = '/usr/lib/perl5/vendor_perl/5.8.0';
$VAR8 = '/usr/lib/perl5/vendor_perl';
$VAR9 = '.';

In case you need to add a new search path to this, it's pretty simple -
take a look:


#!/usr/bin/perl

# use data dumper
use Data::Dumper;

# add new path to include
push(@INC, "/usr/local/myapp/includes/");

# examine data structure
print Dumper @INC;

Here's the result:


$VAR1 = '/usr/lib/perl5/5.8.0/i386-linux-thread-multi';
$VAR2 = '/usr/lib/perl5/5.8.0';
$VAR3 = '/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi';
$VAR4 = '/usr/lib/perl5/site_perl/5.8.0';
$VAR5 = '/usr/lib/perl5/site_perl';
$VAR6 = '/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi';
$VAR7 = '/usr/lib/perl5/vendor_perl/5.8.0';
$VAR8 = '/usr/lib/perl5/vendor_perl';
$VAR9 = '.';
$VAR10 = '/usr/local/myapp/includes';

There's also a %INC hash, which lists all the files which have been
included by the current script, together with their paths.


#!/usr/bin/perl

# use data dumper
use Data::Dumper;

# examine data structure
print Dumper [\%INC];

Here's the output:


$VAR1 = [
{
'warnings.pm' => '/usr/lib/perl5/5.8.0/warnings.pm',
'warnings/register.pm' =>
'/usr/lib/perl5/5.8.0/warnings/register.pm',
'bytes.pm' => '/usr/lib/perl5/5.8.0/bytes.pm',
'Carp.pm' => '/usr/lib/perl5/5.8.0/Carp.pm',
'XSLoader.pm' =>
'/usr/lib/perl5/5.8.0/i386-linux-thread-multi/XSLoader.pm',
'overload.pm' => '/usr/lib/perl5/5.8.0/overload.pm',
'Exporter.pm' => '/usr/lib/perl5/5.8.0/Exporter.pm',
'Data/Dumper.pm' =>
'/usr/lib/perl5/5.8.0/i386-linux-thread-multi/Data/Dumper.pm'
}
];

The difference between @INC and %INC is subtle but important - the former
specifies the list of paths to search for files, while the latter specifies
the files which have already been included in the current script, together
with their paths.

There's also the %ENV hash, which contains a list of available environment
variables.


#!/usr/bin/perl

# use data dumper
use Data::Dumper;

# examine data structure
print Dumper [\%ENV];

Take a look (this is an abridged output sample):


$VAR1 = [
{
'HOME' => '/home/me',
'SSH_CLIENT' => '192.168.0.241 1099 22',
'LESSOPEN' => '|/usr/bin/lesspipe.sh %s',
'MAIL' => '/var/spool/mail/me',
'PWD' => '/home/me',
'LANG' => 'en_US',
'USER' => 'me',
'G_BROKEN_FILENAMES' => '1',
'TERM' => 'xterm',
'SSH_TTY' => '/dev/pts/5'
}
];

Since %ENV is a hash, it's fairly easy to alter an environment setting -
all you have to do is specify a new value for the corresponding hash key.
Consider the following example, which shows you how:


#!/usr/bin/perl

# get value
print "Terminal is $ENV{'TERM'}\n";;

# change value
$ENV{'TERM'} = 'vt100';

# get new value
print "Terminal is now $ENV{'TERM'}\n";;

Here's the output:


Terminal is xterm
Terminal is now vt100



 
 
>>> More Perl Programming Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: