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

    Dell PowerEdge Servers

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

    As you've already seen in last time's lesson, Perl comes with all the standard arithmetic operators - addition [+], subtraction [-], division [/] and multiplication [*] - and quite a few non-standard ones. Here's an example which demonstrates the important ones:
    #!/usr/bin/perl
    # get a number
    print "Gimme a number! ";
    $alpha = <STDIN>;
    # get another number
    print "Gimme another number! ";
    $beta = <STDIN>;
    # process input
    chomp($alpha);
    chomp($beta);
    # standard stuff
    $sum = $alpha + $beta;
    $difference = $alpha - $beta;
    $product = $alpha * $beta;
    $quotient = $alpha / $beta;
    # non-standard stuff
    $remainder = $alpha % $beta;
    $exponent = $alpha ** $beta;
    # display the result
    print "Sum: $sum\n";
    print "Difference: $difference\n";
    print "Product: $product\n";
    print "Quotient from division: $quotient\n";
    print "Remainder from division: $remainder\n";
    print "Exponent: $exponent\n";

    As with all other programming languages, division and multiplication take precedence over addition and subtraction, although parentheses can be used to give a particular operation greater precedence. For example,
    #!/usr/bin/perl
    print(10 + 2 * 4);

    returns 18, while
    #!/usr/bin/perl
    print((10 + 2) * 4);

    returns 48.

    In addition to these operators, Perl also comes with the very useful auto-increment [++] and auto-decrement [--] operators, which you'll see a lot of in the next lesson. For the moment, all you need to know is that the auto-increment operator increments the value of the variable to which it is applied by 1, while the auto-decrement operator does exactly the same thing, but in the opposite direction. Here's an example:
    #!/usr/bin/perl
    # initial value
    $a = 7;
    print("Initial value: ", $a, "\n");
    # increment and display
    $a++;
    print("After increment: ", $a, "\n");

    More Perl Articles
    More By Vikram Vaswani and Harish Kamath, (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