Perl
  Home arrow Perl arrow Page 6 - Understanding Perl's Special Variables
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Understanding Perl's Special Variables
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 34
    2003-07-10


    Table of Contents:
  • Understanding Perl's Special Variables
  • In Default
  • Input...
  • ...And Output
  • Getting Into An Argument
  • The Right Path
  • To Err Is Human
  • A Question Of Ownership
  • Rank And File
  • Calling For A Translator
  • End Zone

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    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



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

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT