Perl
  Home arrow Perl arrow Page 3 - 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 - 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

    - 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