Perl
  Home arrow Perl arrow Page 6 - Understanding Perl's Special Variables
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 4 stars4 stars4 stars4 stars4 stars / 30
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    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

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway