Home > Articles > Web Development > Perl

  • Print
  • + Share This
This chapter is from the book

Answers

Here are the answers to the Workshop questions in the previous section.

Quiz Answers

  1. 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. 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. 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.

  4. 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+'.

  5. 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. 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. 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. 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. The answers are

  10. . concatenates strings

    ** creates exponential numbers

    ne "not equals" for strings

    || logical OR (C-style)

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

Exercise Answers

  1. 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";
  2. The file handle for standard input is STDIN, not INPUT.

  3. 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"; 
  1. 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 . "\n";
print '*' x 80;
  1. 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);
  • + Share This
  • 🔖 Save To Your Account