Contents
8. Literals and Operators
Example: Numbers and Characters
#!/usr/local/bin/perl
print '007',' has been portrayed by at least ', 004, ' actors. ';
print 7+3, ' ', 7*3, ' ', 7/3, ' ', 7%3, ' ', 7**3, ' ';
$x = 7;
print $x;
print ' Doesn\'t resolve variables like $x and backslashes \n. ';
print "Does resolve $x and backslash\n";
$y = "A line containing $x and ending with line feed.\n";
print $y;
$y = "Con" . "cat" . "enation!\n";
print $y;
This produces:
007 has been portrayed by at least 4 actors. 10 21
2.3333333333333335 1 343 7
Doesn't resolve variables like $x and backslashes \n. Does
resolve 7 and backslash
A line containing 7 and ending with line feed.
Concatenation!
Questions:
- Why does the output from the first few print statements run together?
- Is it necessary to declare variables in Perl? Is it possible?
Example: Comparisons
# The following "<<" variation of
# data input simplifies CGI forms.
$x = 'operator';
print <<THATSALL;
A common mistake: Confusing the assignment $x =
and the numeric comparison $x ==, and the character
comparison $x eq.
THATSALL
$x = 7;
if ($x == 7) { print "x is $x\n"; }
if ($x = 5) {
print "x is now $x,",
"the assignment is successful.\n";
}
$x = 'stuff';
if ($x eq 'stuff') {
print "Use eq, ne, lt, gt, etc for strings.\n";
}
This produces:
A common mistake: Confusing the assignment operator =
and the numeric comparison operator ==, and the character
comparison operator eq.
x is 7
x is now 5, the assignment is successful.
Use eq, ne, lt, gt, etc for strings.
Example: Ordinary Arrays
@stuff = ('This', 'is', 'a', 'list.');
print "Lists and strings are indexed from 0.\n";
print "So \$stuff[1] = $stuff[1], ",
"and \$#stuff = $#stuff.\n";
print @stuff,"\n";
print join('...',@stuff),"\n";
splice(@stuff, 3, 0, ('fine', 'little'));
print join('...',@stuff),"\n";
This produces:
Lists and strings are indexed from 0.
So $stuff[1] = is, and $#stuff = 3.
Thisisalist.
This...is...a...list.
This...is...a...fine...little...list.
Homework: Validate a date.
#!/usr/local/bin/perl
print "Enter numeric: month day year\n";
$_ = <STDIN>;
($month,$day,$year) = split;
Complete this program. Print an error
message if the month is not valid. Print an error message if the day is not
valid for the given month (31 is ok for January but not for February). See if
you can avoid using conditionals (if, unless, ?,...) statements but instead use
data structures.
Approach this incrementally. On the first draft, assume that the user enters
3 numbers separated by spaces and that February has 28 days. Subsequent
refinements should account for bad input and leap year. Finally, find a Perl
builtin function that converts a date to system time, and see how to use that to
validate time data generally.
Homework: Play with associative arrays.
Start with a few assignments like:
$name{12345} = 'John Doe';
$name{24680} = 'Jane Smith';
Print these scalars. What is the value of
an associative array element that has never been assigned? What happens if you
assign an associative array to a scalar? What happens if you assign an
associative array to a normal array?
$blunk = %name;
@persons = %name;
print '$blunk=',$blunk,', @persons=',
join(', ',@persons),"\n";
What happens if you assign a normal array
to an associative array?