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

Miscellaneous Notes - 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
Before you go, here are a couple of things that you might find interesting:

chomp() versus chop()
---------------------
In addition to the chomp() function used above, Perl also has a chop() function. While the chomp() function is used to remove the trailing newline if it exists, the chop() function is designed to remove the last character of a variable, irrespective of whether or not it is a newline character.
#!/usr/bin/perl
# set up the variables
$sacrificial_goat1 = "boom";
$sacrificial_goat2 = "boom";
# chomp it!
chomp($sacrificial_goat1);
print($sacrificial_goat1, "\n");
# chop it!
chop($sacrificial_goat2);
print($sacrificial_goat2, "\n");

Assignment operators versus equality operators
----------------------------------------------
An important point to note - and one which many novice programmers fall foul of - is the difference between the assignment operator [=] and the equality operator [==]. The former is used to assign a value to a variable, while the latter is used to test for equality in a conditional expression.

So
$a = 47;

assigns the value 47 to the variable $a, while
$a == 47

tests whether the value of $a is equal to 47.

Special characters and print()
------------------------------
As you've seen, print() can be used in either one of two ways:
#!/usr/bin/perl
$day = "Tuesday";
print("Today is ", $day);

or
#!/usr/bin/perl
$day = "Tuesday";
print "Today is $day";

both of which are equivalent, and return this output:
Today is Tuesday

But now try replacing the double quotes with singles, and watch what happens:
#!/usr/bin/perl
$day = "Tuesday";
print 'Today is $day';

Your output should now read
Today is $day

Thus, single quotes turn off Perl's "variable interpolation" - simply, the ability to replace variables with their actual value when executing a program. This also applies to special characters like the newline character - single quotes will cause Perl to print the newline character as part of the string, while double quotes will allow it to recognize the character correctly.

You should note this difference in behaviour, if only to save yourself a few minutes of debugging time.

The second thing to note about print() is that you need to "escape" special characters with a backslash. Take a look at this example:
#!/usr/bin/perl
print("She said "Hello" to me, and my 
heart skipped a beat.");

When you run this, you'll see a series of error messages - this is because the multiple sets of double quotes within the print() function call confuse Perl. And so, if you'd like your output to contain double quotes [or other special characters], it's necessary to escape them with a preceding backslash.
#!/usr/bin/perl
print("She said \"Hello\" to me, and my 
heart skipped a beat.");

As to what happens next - you'll have to wait for the next lesson in this series, when we'll be teaching you a few more control structures and introducing you to the different types of loops supported by Perl. See you then!

This article copyright Melonfire 2000. All rights reserved.

 
 
>>> 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: