Perl
  Home arrow Perl arrow Perl 101 (Part 2) - Of Variables And O...
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

Perl 101 (Part 2) - Of Variables And Operators
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

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

    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

    - 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
    - Perl: Releasing Your Inner Textuality




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway