Perl
  Home arrow Perl arrow 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
    ( Page 1 of 8 )

    Now that you've got the basics of the language down, this second article in the series teaches you about Perl's variables and operators, and also introduces you to conditional expressions.In the first part of this tutorial, we introduced you to the basics of Perl, Austin Powers-style. This week, we're going to get down and dirty with variables and operators, and also provide you with a brief introduction to Perl's conditional expressions.

    To begin with, let's answer a very basic question for all those of you unfamiliar with programming jargon: what's a variable when it's at home?

    A variable is the fundamental building block of any programming languages. Think of a variable as a container which can be used to store data; this data is used in different places in your Perl program. A variable can store both numeric and non-numeric data, and the contents of a variable can be altered during program execution. Finally, variables can be compared with each other, and you - the programmer - can write program code that performs specific actions on the basis of this comparison.

    Every language has different types of variables - however, for the moment, we're going to concentrate on the simplest type, referred to in Perl as "scalar variables". A scalar variable can hold any type of value - integer, text string, floating-point number - and is usually preceded by a dollar sign.

    The manner in which scalar variables are assigned values should be clear from the following example:
    #!/usr/bin/perl
    
    # a simple scalar variable
    $name = "mud";
    
    # and an example of how to use it
    print ("My name is ", $name);

    And here's what the output of that program looks like:
    My name is mud

    Really?! We feel for you...

    It shouldn't be too hard to see what happened here - the scalar variable $name was assigned the text value "mud", and this value was then printed to the console via the print() statement.

    Although assigning values to a scalar variable is extremely simple - as you've just seen - there are a few things that you should keep in mind here:
    • A scalar variable name must be preceded by a dollar [$] sign - for example, $name, $id and the like. This helps differentiate between scalar variables and arrays or hashes, which are the other types of variables supported by Perl.
    • Every scalar variable name must begin with a letter, optionally followed by more letters or numbers - for example, $a, $data123, $i_am_god
    • The maximum length of a scalar variable name is 255 characters; however, if you use a name that long, you need therapy!
    • Case is important when referring to scalar variables - in Perl, a $cigar is definitely not a $CIGAR!
    • It's always a good idea to give your variables names that make sense and are immediately recognizable - it's easy to tell what $gross_income refers to, but not that easy to identify $ginc.
    • Unlike other programming languages, a scalar variable in Perl can store both integers and floating-point numbers [also known as decimals]. This added flexibility is just one of the many nice things about Perl.
    Here's another program, this one illustrating how variables can be used to store and manipulate numbers.
    #!/usr/bin/perl
    
    # declare a variable
    $number = 19;
    
    print ($number, " times 1 is ", $number,"\n");
    print ($number, " times 2 is ", $number * 2, "\n");
    print ($number, " times 3 is ", $number * 3, "\n");
    print ($number, " times 4 is ", $number * 4, "\n");
    print ($number, " times 5 is ", $number * 5, "\n");

    And here's what the output looks like:
    19 times 1 is 19
    19 times 2 is 38
    19 times 3 is 57
    19 times 4 is 76
    19 times 5 is 95



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