Perl
  Home arrow Perl arrow Page 4 - Understanding Perl's Special Variables
The Best Selling PC Migration Utility.
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 
IBM Developerworks
 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Understanding Perl's Special Variables - ...And Output
    (Page 4 of 11 )

    The reverse of the input record separator is the output record separator,
    quite logically found in the $\ variable. While the $/ variable deals with
    the delimiter used by Perl to break input into discrete records, the $\
    variable controls which delimiter Perl uses to separate multiple print()
    invocations.

    By default, the output record separator is null, which means that the
    output from every call to print() gets attached to the output from the
    previous call. Consider the following example, which demonstrates:


    #!/usr/bin/perl

    # print output
    print "The";
    print "cow";
    print "jumped";
    print "over";
    print "the";
    print "moon";

    Here's the output:


    Thecowjumpedoverthemoon

    You can alter this by specifying a different value from the $\ variable -
    as the following example does:


    #!/usr/bin/perl

    # set output record separator
    $\ = " -?- ";

    # print output
    print "The";
    print "cow";
    print "jumped";
    print "over";
    print "the";
    print "moon";

    Here's the result:


    The -?- cow -?- jumped -?- over -?- the -?- moon -?-

    Similar, though not identical, is the output field separator, which is used
    to specify the delimiter between the different values specified in a single
    print() command. This value is stored in the $, variable, and is usually
    null. Consider the following example, which demonstrates how it can be used:


    #!/usr/bin/perl

    # set output field separator
    $, = ":";

    # print output
    print "The", "cow", "jumped", "over", "the", "moon";

    Here's the output:


    The:cow:jumped:over:the:moon

    A common use of these two variables is to customize the output of the
    print() command - as in the following example:


    #!/usr/bin/perl

    # load module
    use DBI();

    # connect
    my $dbh = DBI->connect("DBI:mysql:database=test;host=localhost", "root",
    "secret", {'RaiseError' => 1}) or die ("Cannot connect to database");

    # query
    my $sth = $dbh->prepare("SELECT name, age, sex FROM users");
    $sth->execute();

    # set separators
    $, = ":";
    $\ = "\r\n";

    # print data as colon-separated fields
    # each record separated by carriage return
    while(my $ref = $sth->fetchrow_hashref())
    {
    print $ref->{'name'}, $ref->{'age'}, $ref->{'sex'};
    }

    # clean up
    $dbh->disconnect();

    Here's the output:


    john:34:M
    jimmy:21:M
    john:31:F
    jane:27:F

    Sure, this is a very complicated way of doing something really simple - but
    hey, it's an example. Don't take it too seriously!

    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 6 hosted by Hostway