$inputline = <STDIN>; print ($inputline);
Each of the tokens in the above code, as well as many others, will be explained in the following sections.
$a = 5; $a = "hello";
The first line is an example of a variable holding a numeric literal value - the number 5. The second line is an example of a string literal value - the word hello. For short, these are referred to as numeric variables and string variables.
Perl can treat variable values differently according to context - for example:
$a = 1; $b = 2; print $a + $b; # prints 3 print "\n"; print $a . $b; # prints 12
$value = 9.01e+21 + 0.01 - 9.01e+21;
print ("first value is ", $value, "\n"); # yields 0
$value = 9.01e+21 - 9.01e+21 + 0.01;
print ("second value is ", $value, "\n"); #yields 0.01
An example:
$name = "Mike"; print "hello $name\n"; print qq/hello $name\n/; print 'hello $name\n'; print q/hello $name\n/;
"the ${number}th value". This way Perl will print the value of $number and not think you mean variable $numberth.
$result = 4 + 2 * 3; print $result;
$result = 2 ** 3 ** 2; print $result;
$a *= 2; is the same as $a = $a * 2;
$a = $b = 4;
The angle operator - also known as the diamond operator - is used only with file handles. It's used to read a line of input from a file.
Here's the example again from the start of class, reading a line of input from STDIN:
$inputline = <STDIN>; print ($inputline);
$var1 = 43; $var2 = ++$var1; print $var2;
$var2 equals 44. $var1 was incremented, and then assigned to $var2
$var1 = 43; $var2 = $var1++; print $var2;
$var2 equals 43 - $var1 was assigned to $var2, and then $var1 was incremented.
The decrement operator performs subtraction the same way: $a-- and --$a.
$string1 = "potato"; $string2 = "head"; $string3 = $string1 . $string2; print $string3;
The values of $string1 and $string2 are unchanged. You'll use this one a lot; e.g. getting data from a database and dropping it into a sentence.
There's also the string concatenation and assignment operator:
$a = "potato"; $a .= "head"; print $a;
Formally stated, an expression is a collection of Perl operators and operands yielding a result when evaluated by the Perl interpreter. A Perl statement will contain a single expression. For example, the Perl statement print 1 + 2; contains the function print, the expression 1 + 2, and a semicolon.
You can have an expression that consists of subexpressions. For example: print (2 * 3) + (4 / 2);
$a = 5; $c = $a + $b; print $c . "\n";
Perl warns you that the value of $b is "uninitialized" (which is another word for undefined). Note that this does not break the script. If you try to use a variable that's undefined, Perl pretends its value is null, as it did here.
Now that we know how Perl recognizes true vs. false conditions, there are more operators we can learn.
These are easiest to explain with examples:
$a = 5; $b = 0; print "logical or:\n"; print $a || $b; print "\nlogical not:\n"; print ! $a; print "\nlogical and:\n"; print $a && $b;
With the "logical or" operator, Perl looks for at least one "true" value in the arguments supplied. $a is considered "true" since it meets the test of truth outlined above. Perl employs "short-circuit" evaluation with the logical or operator (this means it stops evaluating as soon as it finds a true value). It then returns the value of the last argument that was evaluated - in this case, the value of $a.
In the "logical not" example, the value of $a is "true", but we're asking for "not $a" which means we're asking for "false." Perl will answer such a request with an empty string.
In the "logical and" example, we're asking if all of the arguments supplied are true. As with the logical or operator, Perl employs "short circuit" evaluation. This means that if all the arguments are true, it will return the value of the last (right-most) argument, since that is the last one Perl evaluated. If one or more of them is false, it will return the value of the first false value encountered, as it immediately stops evaluating at that point.
All of these return true or false, except for the <=> and cmp operators which return 1 if the left-hand argument is greater, 0 if the two arguments are equal, and -1 if the right-hand argument is greater.
print 4 > 5; # returns false (an empty string) print "\n"; print 5 > 4; # returns true (the number one)Another example:
print 4 <=> 3; # returns 1 print "\n"; print 4 <=> 4; # returns 0 print "\n"; print 3 <=> 4; # returns -1
"a" == "b"; returns true, and so does 2 gt 10
$var = 5; $result = $var == 5 ? 14 : 7; print $result;
This means: If $var equals 5, then $result equals 14; otherwise $result equals 7. In this example, $result equals 14.
| OPERATOR | SYMBOLS | NOTES |
| q & qq Operators | q qq | Variable interpolation; Escape characters; User-defined delimiters |
| Arithmetic | ** * / % + - | Precedence; Associativity |
| Assignment | = += -= *= /= %= | Cascades - e.g. $a = $b = 4; |
| Angle | <> | Used only with file handles |
| Increment & Decrement | ++ -- | Prefix and Postfix forms |
| String Concatenation | . .= | |
| 1 Argument | - int length lc uc cos rand | Used with only a single argument |
| Logical | && || ! | "Short-circuit" evaluation; Returns last expression evaluated |
| Numeric Comparison | < > == <= >= != <=> | Returns true (1) or false (null string); Comparison operator returns 1, 0, or -1 |
| String Comparison | lt gt eq le ge ne cmp | Ditto above; Alphabetic comparisons |
| Bit-manipulation | & | ^ ~ << >> | Used on base 2 (binary) numbers |
| Conditional | x ? y : z | if x is true, return y; otherwise, return z |
So far you've seen the "print" function. When the Perl interpreter sees "print" in your code, it invokes the "print" function from the Perl library of functions. Most functions accept an argument, which tell Perl what to do with the invoked function (print accepts an argument consisting of what you want to print). Some functions accept multiple arguments.
We'll learn plenty of functions over the course of the semester. Here's a few besides print:
print "please enter your name: "; $input = <STDIN> print "hello " . $input . ", how are you?\n"; chomp($input); print "goodbye " . $input .", have a nice day.\n";The return value of chomp is true or false, indicating whether the chomp succeeded.
$a = 4; $b = 2; $b > $a || die "the script ends here"; print "goodbye\n";
Perl will append the filename and line number to the end of the string argument you pass to die.
Note that in this example, we're using the "logical or" operator like a conditional (i.e. we only run die if $b is greater than $a).
$number = 5;
if ($number > 0) {
print "the number is greater than zero.\n";
}
The part preceding the opening curly bracket is known as the conditional expression. The part within the curly brackets is one or more statements constituting the statement block. When using if, the statement block is executed if the conditional statement yields a true result. The statement block can be empty.
$number = 0;
if ($number > 0) {
print "the number is greater than zero.\n";
}
else {
print "the number is zero.\n";
}
$number = -2;
if ($number > 0 ) {
print "the number is greater than zero.\n";
}
elsif ($number < 0) {
print "the number is less than zero.\n";
}
else {
print "the number is zero.\n";
}
$number = 0;
unless ($number) {
print "the expression is false.\n";
}
$count = 0;
while ($count <= 5) {
print "count is " . $count++ . "\n";
}
This will result in the numbers 0 to 5 being printed to your screen. Try it with a pre-increment operator instead.
$count = 0;
until ($count > 5) {
print "count is $count\n";
$count++;
}
This will result in the same thing as the preceding while loop.
$name = "mike";
print ("hello\n") if ($name eq "mike");
$count = 0;
print ("the number is $count\n") while ($count++ < 5);
Each of the questions below ask you to write a script. Write your scripts using Windows Notepad. Save them to your floppy disk in the A: drive. Make sure you save them with a .pl file extension. If you do not have a floppy disk, please save your files to the C:\temp directory. Run your scripts from the MS-DOS Prompt as we learned last week.
Things to remember: