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

1:What are the two types of data you can use in Perl?
A1: The two types of data you can use in Perl are scalar data, for individual things such as numbers and strings and list data, for collective things such as arrays.
2:What kinds of data make up scalar data?
A2: Numbers, strings, and references. Full credit if you only said numbers and strings; we haven't talked about references yet.
3:What are the differences between double- and single-quoted strings?
A3: There are two differences between single- and double-quoted strings:
  • Double-quoted strings can contain any number of special character escapes; single-quoted strings can only contain ' and \.

  • Double-quoted strings will interpolate variables inside them (that is, replace the variables with their values).

4:Which of the following are valid scalar variables?
$count
$11foo
$_placeholder
$back2thefuture
$long_variable_to_hold_important_value_for_later

A4: All the variables in that list except for $11foo are valid. $11foo is invalid because it starts with a number (Perl does have variables that start with numbers, but they're all reserved for use by Perl).
5:What's the difference between the = and == operators?
A5: The = operator is the assignment operator, to assign a value to a variable. The == operator is for testing equality between numbers.
6:What's the difference between a statement and an expression?
A6: A statement is a single operation in Perl. An expression is a statement that returns a value; you can often nest several expressions inside a single Perl statement.
7:What does this expression evaluate to: 4 + 5 / 3**2 * 6?
A7: 7.33333333333333, give or take a 3 or two.
8:How do you round off numbers in Perl?
A8: To round off numbers without printing them, use sprintf. To print numbers with less precision, use printf.
9:What are the values that Perl considers false?
A9: Perl has three false values: 0, the empty string "", and the undefined value.
10:Why are there different operators for number and string comparisons?
A10: Perl has different operators for numbers and strings because of its capability to auto-convert scalar values and the differences in handling both those values.
11:Define what a short-circuiting logical operator does.
A11: Short-circuiting operators only evaluate their right-side operands when necessary. If the value of the left-side operator determines the overall value of the expression (for example, if the left side of a && operator is false), the expression will stop.
12:How is a pattern-matching test different from an equality test?
A12: Equality tests return true if the scalars on both sides of the operator are exactly equal. Pattern matching tests result true of the scalar on the left side of the operator contains characters that match the pattern on the right side of the operator.
13:You have a pattern /ing/. Which of the following strings does this pattern match to?
'Singapore'
'viking'
'vainglorious'
'intermingle'
'Westinghouse'
'Ingmar'
									

A13: All the given strings match the pattern /ing/ except for the last one, 'Ingmar', which does not match because of the capital I. If the pattern /ing/ had an i at the end (/ing/i) then it would have matched the last pattern, as well.

Exercises

1:Modify temp.pl to convert Celsius back to Fahrenheit.
A1: Here's one answer:
#!/usr/local/bin/perl -w

$cel = 0;
$fahr = 0;
print 'Enter a temperature in Celsius: ';
chomp ($cel = <STDIN>);
$fahr = $cel * 9 / 5 + 32;
print "$cel degrees Celsius is equivalent to ";
printf("%d degrees Fahrenheit 
", $fahr);

2:Write a program that prompts you for the width and length of a room, and then prints out the square footage.
A2: Here's one answer:
#!/usr/local/bin/perl -w

$width = 0;
$length = 0;
$sqft = 0;

print 'Enter the width of the room (feet): ';
chomp ($width = <STDIN>);
print 'Enter the length of the room (feet): ';
chomp ($length = <STDIN>);
$sqft = $width * $length;
print "The room is $sqft square feet.
";

3:BUG BUSTERS: What's wrong with this program?
print 'Enter the word Foo: ';
chomp($input = <STDIN>);
if ($input = 'foo') {
   print "Thank you!
";
}  else {
   print "That's not the word foo.
";
}

A3: The test inside the parentheses for the if statement uses an assignment operator instead of an equality operator. This test will always return true.
4:BUG BUSTERS: How about this version?
print 'Enter the word foo: ';
chomp($input = <STDIN>);
if ($input == 'foo') {
   print "Thank you!
";
}  else {
   print "That's not the word foo.
";
}

A4: This time, the test inside the parentheses has a number equality test; because this test compares strings, you need an eq test instead.
5:Modify Exercise 6 from yesterday (the combination of hello world and cookie) so that it will exit if you type any of the following: goodbye, good-bye, bye-bye, ok bye, or just bye.
A5: Here's one answer:
#!/usr/local/bin/perl -w

$name = "";

while ( $name !~ /bye/ ) {
  print 'Enter your name: ';
  chomp($name = <STDIN>);
  print "Hello, ";
  print $name;
  print "!
";
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset