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's the difference between the postfix increment operator ($x++) and the prefix increment operator (++$x)?
A1: 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.
2:What does operator precedence determine? How about associativity?
A2: 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.
3:What is the difference between the following patterns?
/d/
/d/
/D/
/dd/

A3: The patterns are as follows:
  • /d/ matches the single character d.

  • /d/ matches a single digit.

  • /D/ matches a single nondigit character.

  • /dd/ matches a d followed by a single digit.

4:You have the pattern /ddD/. Which of the following strings does this pattern match to?
'456'
'd55'
'55+'
'4aa'
'4b'

A4: The pattern /ddD/ matches two digits and one nondigit, in that order. None of the given strings match the pattern except for '55+'.
5:What is a file handle? Why do you need one?
A5: 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.
6:Define standard input and output. What are they used for?
A6: 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.
7:What does the chomp function do?
A7: 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.
8:What are the differences between print, printf, and sprintf? When would you use each one?
A8: 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.
9:What do the following operators do?
.
**
ne
||
*=
									

A9: The answers are

. concatenates strings

** creates exponential numbers

ne “not equals” for strings

|| logical OR (C-style)

*= Multiply and assign; same as $x = $x * $y

Exercises

1: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.
A1: 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
";

2:BUG BUSTER: What's wrong with this bit of code?
while () {
 print 'Enter a name: ';
 chomp ($input = <INPUT>);
 if ($input ne '') {
   $names++;
}
 else { last; }
}

A2: The file handle for standard input is STDIN, not INPUT.
3:Write a program that accepts input as multiple words on different lines, and combines those words into a single string.
A3: 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
";

4: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.
A4: Here's one answer:
#!/usr/local/bin/perl -w

$input = ""; # temporary input
$space = 0; # space around
$length = 0 ; # length of string

print 'Enter some text: ';
chomp ($input = <STDIN>);
$length = length $input;
$space = int((80 - $length) / 2);
print ' ' x $space;
print $input;
print ' ' x $space . "
";
print '*' x 80;

5: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!
A5: 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.
";
        next;
    }

    last;
}

$cel = ($fahr - 32) * 5 / 9;
print "$fahr degrees Fahrenheit is equivalent to ";
printf("%.0f degrees Celsius
", $cel);

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

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