Appendix A. Answers to Exercises

This appendix contains the answers to the exercises presented throughout the book.

Answers for Chapter 2

Exercise 1 (Section 2.10.1)

Here’s one way to do it. First, start with the package directive and use strict:

package Oogaboogoo::date;
use strict;

Then define the constant arrays to hold the mappings for day-of-week and month names:

my @day = qw(ark dip wap sen pop sep kir);
my @mon = qw(diz pod bod rod sip wax lin sen kun fiz nap dep);

Next, define the subroutine for day-of-week-number to name. Note that this subroutine will be accessible as Ooogaboogoo::date::day:

sub day {
  my $num = shift @_;
  die "$num is not a valid day number"
    unless $num >= 0 and $num <= 6;

Similarly, you have the subroutine for the month-of-year-number to name:

sub mon {
  my $num = shift @_;
  die "$num is not a valid month number"
    unless $num >= 0 and $num <= 11;

Finally, the mandatory true value at the end of the package:


Name this file within a directory of Oogaboogoo in one of the directories given in your @INC variable, such as the current directory.

Exercise 2 (Section 2.10.2)

Here’s one way to do it. Pull in the .pl file from a place in your @INC path:

use strict;
require 'Oogaboogoo/';

Then get the information for the current time:

my($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime;

Then use the newly defined subroutines for the conversions:

my $day_name = Oogaboogoo::date::day($wday); my $mon_name = Oogaboogoo::date::mon($mon); ...

Get Learning Perl Objects, References, and Modules now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.