Understanding Perl's Special Variables - The Right Path (Page 6 of 11 )
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
Next: To Err Is Human >>
More Perl Articles
More By icarus, (c) Melonfire