Perl
  Home arrow Perl arrow Page 8 - Perl 101 (Part 2) - Of Variables And Operators
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

Perl 101 (Part 2) - Of Variables And Operators
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2000-06-01


    Table of Contents:
  • Perl 101 (Part 2) - Of Variables And Operators
  • Q
  • 2 2 ...
  • ... Or Two Plus Two
  • Comparing Apples And Oranges
  • Decisions! Decisions!
  • Handling The Gray Areas
  • Miscellaneous Notes

  • 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


    Perl 101 (Part 2) - Of Variables And Operators - Miscellaneous Notes
    ( Page 8 of 8 )

    Before you go, here are a couple of things that you might find interesting:

    chomp() versus chop()
    ---------------------
    In addition to the chomp() function used above, Perl also has a chop() function. While the chomp() function is used to remove the trailing newline if it exists, the chop() function is designed to remove the last character of a variable, irrespective of whether or not it is a newline character.
    #!/usr/bin/perl
    
    # set up the variables
    $sacrificial_goat1 = "boom";
    $sacrificial_goat2 = "boom";
    
    # chomp it!
    chomp($sacrificial_goat1);
    print($sacrificial_goat1, "\n");
    
    # chop it!
    chop($sacrificial_goat2);
    print($sacrificial_goat2, "\n");

    Assignment operators versus equality operators
    ----------------------------------------------
    An important point to note - and one which many novice programmers fall foul of - is the difference between the assignment operator [=] and the equality operator [==]. The former is used to assign a value to a variable, while the latter is used to test for equality in a conditional expression.

    So
    $a = 47;

    assigns the value 47 to the variable $a, while
    $a == 47

    tests whether the value of $a is equal to 47.

    Special characters and print()
    ------------------------------
    As you've seen, print() can be used in either one of two ways:
    #!/usr/bin/perl
    
    $day = "Tuesday";
    print("Today is ", $day);

    or
    #!/usr/bin/perl
    
    $day = "Tuesday";
    print "Today is $day";

    both of which are equivalent, and return this output:
    Today is Tuesday

    But now try replacing the double quotes with singles, and watch what happens:
    #!/usr/bin/perl
    
    $day = "Tuesday";
    print 'Today is $day';

    Your output should now read
    Today is $day

    Thus, single quotes turn off Perl's "variable interpolation" - simply, the ability to replace variables with their actual value when executing a program. This also applies to special characters like the newline character - single quotes will cause Perl to print the newline character as part of the string, while double quotes will allow it to recognize the character correctly.

    You should note this difference in behaviour, if only to save yourself a few minutes of debugging time.

    The second thing to note about print() is that you need to "escape" special characters with a backslash. Take a look at this example:
    #!/usr/bin/perl
    
    print("She said "Hello" to me, and my 
    heart skipped a beat.");

    When you run this, you'll see a series of error messages - this is because the multiple sets of double quotes within the print() function call confuse Perl. And so, if you'd like your output to contain double quotes [or other special characters], it's necessary to escape them with a preceding backslash.
    #!/usr/bin/perl
    
    print("She said \"Hello\" to me, and my 
    heart skipped a beat.");

    As to what happens next - you'll have to wait for the next lesson in this series, when we'll be teaching you a few more control structures and introducing you to the different types of loops supported by Perl. See you then!

    This article copyright Melonfire 2000. All rights reserved.

     
     
    >>> More Perl Articles          >>> More By Vikram Vaswani and Harish Kamath, (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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek