Home arrow Perl Programming arrow Page 2 - Perl 101 (Part 2) - Of Variables And Operators

Q&A - Perl

Now that you've got the basics of the language down, this secondarticle in the series teaches you about Perl's variables and operators, andalso introduces you to conditional expressions.

TABLE OF CONTENTS:
  1. Perl 101 (Part 2) - Of Variables And Operators
  2. Q
  3. 2 2 ...
  4. ... Or Two Plus Two
  5. Comparing Apples And Oranges
  6. Decisions! Decisions!
  7. Handling The Gray Areas
  8. Miscellaneous Notes
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
June 01, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: