Perl Programming Page 8 - Perl 101 (Part 2) - Of Variables And Operators |
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.
blog comments powered by Disqus |
|
|
|
|
|
|
|