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

    PCmover - $15 Off with Coupon Code CJPH7Q

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

    Thus far, you've been assigning values to your variables at the time of writing the program itself [referred to in geek lingo as "design time"]. In the real world, however, many variables are assigned only after the program is executed [referred to as "run time"]. And so, this next program allows you to ask the user for input, assign this input to a variable, and then perform specific actions on that variable.
    #!/usr/bin/perl
    # ask a question...
    print "Gimme a number! ";
    # get an answer...
    $number = <STDIN>;
    # process the answer...
    chomp($number);
    $square = $number * $number;
    # display the result
    print "The square of $number is $square\n";

    If you try it out, you'll see something like this:
    Gimme a number! 4
    The square of 4 is 16

    Let's go through this in detail. The first line of the program prints a prompt, asking the user to enter a number. Once the user enters a number, that input is assigned to the variable $number via the <STDIN> file handler. This particular file handler allows you to access data entered by the user at the command prompt, also referred to as STanDard INput.

    At this point in time, the variable $number contains the data entered by you at the prompt, together with a newline [\n] character [caused by you hitting the Enter key]. Before the number can be processed, it is important that you remove the newline character, as leaving it in could adversely affect the rest of your program. Hence, chomp().

    The chomp() function's sole purpose is to remove the newline character from the end of a variable, if it exists. Once that's taken care of, the number is multiplied by itself, and the result is displayed. Note our slightly modified usage of the print() function in this example - instead of using parentheses, we're simply printing a string and replacing the variables in it with their actual values.

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