- 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
Workshop
The workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try and understand the quiz and exercise answers before you go on to tomorrow's lesson.
Quiz
-
What's the difference between the postfix increment operator ($x++) and the prefix increment operator (++$x)?
-
What does operator precedence determine? How about associativity?
-
What is the difference between the following patterns?
- You have the pattern /\d\d\D/. Which of the following strings does
this pattern match to?
'456'
'd55'
'55+'
'4aa'
'4b'
-
What is a file handle? Why do you need one?
-
Define standard input and output. What are they used for?
-
What does the chomp function do?
-
What are the differences between print, printf, and sprintf? When would you use each one?
-
What do the following operators do?
/d/
/\d/
/\D/
/d\d/
.
**
ne
||
*=
Exercises
-
Write a program that accepts any number of lines of any kind of input, ending with a Return or Enter (similarly to how the stats.pl program works). Return the number of lines that were entered.
-
BUG BUSTER: What's wrong with this bit of code?
while () {print 'Enter a name: ';chomp ($input = <INPUT>);if ($input ne '') {$names++;}else { last; }}
-
Write a program that accepts input as multiple words on different lines, and combines those words into a single string.
-
Write a program that takes a string and then centers it on the screen (assume an 80-character line, and that the string is less than 80 characters). Hint: the length function will give you the length in characters of a string.
-
Modify the Fahrenheit to Celsius converter program from yesterday to make sure that the input is a valid digit. If the user enters something invalid, loop until the input is valid. Watch out for negative numbers!