Perl
  Home arrow Perl arrow Page 3 - 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? 
Google.com  
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 - Input...
    ( Page 3 of 11 )

    Another very useful variable is the $/ variable, which Perl calls the input
    record separator. The $/ variable contains one or more characters that
    serve as line delimiters for Perl; Perl uses this variable to identify
    where to break lines in a file.

    In order to illustrate, consider the following sample password file:


    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    news:x:9:13:news:/etc/news:
    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin

    By default, the $/ variable is set to the newline character. Therefore,
    when Perl reads a file like the one above, it will use the newline
    character to decide where each line ends - as illustrated in the following
    example:


    #!/usr/bin/perl

    # read file into array
    open (FILE, "dummy.txt");
    @lines = <FILE>;

    # iterate over file and print each line
    foreach $line (@lines)
    {
    print "--- " . $line;
    }

    # print number of lines in file
    $count = @lines;
    print "$count lines in file!\n";

    Here's the output:


    --- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    --- halt:x:7:0:halt:/sbin:/sbin/halt
    --- mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    --- news:x:9:13:news:/etc/news:
    --- uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    --- operator:x:11:0:operator:/root:/sbin/nologin
    --- games:x:12:100:games:/usr/games:/sbin/nologin
    --- gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    --- ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    --- nobody:x:99:99:Nobody:/:/sbin/nologin
    10 lines in file!

    Now, if you alter the input record separator, Perl will use a different
    delimiter to identify where each line begins. Consider the following
    example, which sets a colon (:) to be used as the delimiter, and its
    output, which illustrates the difference:


    #!/usr/bin/perl

    # set input record separator
    $/ = ":";

    # read file into array
    open (FILE, "dummy.txt");
    @lines = <FILE>;

    # iterate over file and print each line
    foreach $line (@lines)
    {
    print "--- " . $line;
    }

    # print number of lines in file
    $count = @lines;
    print "$count lines in file!\n";

    Here's the new output:


    --- shutdown:--- x:--- 6:--- 0:--- shutdown:--- /sbin:--- /sbin/shutdown
    halt:--- x:--- 7:--- 0:--- halt:--- /sbin:--- /sbin/halt
    mail:--- x:--- 8:--- 12:--- mail:--- /var/spool/mail:--- /sbin/nologin
    news:--- x:--- 9:--- 13:--- news:--- /etc/news:---
    uucp:--- x:--- 10:--- 14:--- uucp:--- /var/spool/uucp:--- /sbin/nologin
    operator:--- x:--- 11:--- 0:--- operator:--- /root:--- /sbin/nologin
    games:--- x:--- 12:--- 100:--- games:--- /usr/games:--- /sbin/nologin
    gopher:--- x:--- 13:--- 30:--- gopher:--- /var/gopher:--- /sbin/nologin
    ftp:--- x:--- 14:--- 50:--- FTP User:--- /var/ftp:--- /sbin/nologin
    nobody:--- x:--- 99:--- 99:--- Nobody:--- /:--- /sbin/nologin
    61 lines in file!

    You can even undef() the record separator, which will result in Perl
    reading the entire file into a single string. Take a look:


    #!/usr/bin/perl

    # remove input record separator
    undef($/);

    # read file into array
    open (FILE, "dummy.txt");
    @lines = <FILE>;

    # iterate over file and print each line
    foreach $line (@lines)
    {
    print "--- " . $line;
    }

    # print number of lines in file
    $count = @lines;
    print "$count lines in file!\n";

    And here's the new output, which clearly displays that the file has been
    read as a single line:


    --- shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    news:x:9:13:news:/etc/news:
    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    1 lines in file!



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek