- Assignment Operators
- Increment and Decrement Operators
- String Concatenation and Repetition
- Operator Precedence and Associativity
- Using Patterns to Match Digits
- An Example: Simple Statistics
- Input and Output
- Another Example: Stocks
- A Note About Using Functions
- Going Deeper
- Summary
- Q&A
- Workshop
- Answers
Answers
Here are the answers to the Workshop questions in the previous section.
Quiz Answers
-
The difference in prefix and postfix operators is when the variable reference is used and when its value is evaluated. Prefix operators increment the value before using it; postfix increments the variable afterward.
-
Operator precedence determines which parts of an expression are evaluated first given expressions that contain other expressions. Associativity determines the order in which operators that have the same precedence are evaluated.
-
The patterns are as follows:
-
/d/ matches the single character d.
-
/\d/ matches a single digit.
-
\/D/ matches a single nondigit character.
-
/d\d/ matches a d followed by a single digit.
-
The pattern /\d\d\D/ matches two digits and one nondigit, in that order. None of the given strings match the pattern except for '55+'.
-
A file handle is used to read data from or write data to a source or a destination, be it a file, the keyboard, the screen, or some other device. File handles provide a common way for Perl to handle input and output with all those things.
-
Standard input and output are generic input sources and output destinations (that is, not specifically from or to files). They are most commonly used to get input from the keyboard or to print it to the screen.
-
The chomp function removes the newline from the end of a string. If there is no newline on the end of the string, chomp does nothing.
-
The print function is the general way of printing output to the screen or to some other output destination. The printf function prints formatted strings to some output destination; sprintf formats a string, but then simply returns that formatted string value instead of printing it.
-
The answers are
- *= Multiply and assign; same as $x = $x * $y
. concatenates strings
** creates exponential numbers
ne "not equals" for strings
|| logical OR (C-style)
Exercise Answers
- Here's one answer:
#!/usr/local/bin/perl -w
$input = ''; # temporary input
$lines = 0; # count of lines
while () {
print 'Enter some text: ';
chomp ($input = <STDIN>);
if ($input ne '') {
$lines++;
}
else { last; }
}
print "Total number of lines entered: $lines\n";
-
The file handle for standard input is STDIN, not INPUT.
- Here's one answer:
#!/usr/local/bin/perl -w
$input = ''; # temporary input$sent = ''; # final sentence;while () {print 'Enter a word: ';chomp ($input = <STDIN>);if ($input ne '') {$sent .= $input . ' ';}else { last; }}print "Final sentence: $sent\n";
- Here's one answer:
#!/usr/local/bin/perl -w$input = ""; # temporary input$space = 0; # space around$length = 0 ; # length of stringprint 'Enter some text: ';chomp ($input = <STDIN>);$length = length $input;$space = int((80 - $length) / 2);print ' ' x $space;print $input;print ' ' x $space . "\n";print '*' x 80;
- Here's one answer:
#!/usr/local/bin/perl -w
$fahr = 0;
$cel = 0;
while () {
print 'Enter a temperature in Fahrenheight: ';
chomp ($fahr = <STDIN>);
if ($fahr =~ /\D/ and $fahr !~ /-/) {
print "Digits only, please.\n";
next;
}
last;
}
$cel = ($fahr - 32) * 5 / 9;
print "$fahr degrees Fahrenheit is equivalent to ";
printf("%.0f degrees Celsius\n", $cel);