Section A.14. Answers for Chapter 15


A.14. Answers for Chapter 15

A.14.1. Exercise 1

The module Oogaboogoo/date.pm looks like this:

 package Oogaboogoo::date; use strict; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(day mon); 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); sub day {   my $num = shift @_;   die "$num is not a valid day number"     unless $num >= 0 and $num <= 6;   $day[$num]; } sub mon {   my $num = shift @_;   die "$num is not a valid month number"     unless $num >= 0 and $num <= 11;   $mon[$num]; } 1; 

The main program now looks like this:

 use strict; use Oogaboogoo::date qw(day mon); my($sec, $min, $hour, $mday, $mon, $year, $wday) = localtime; my $day_name = day($wday); my $mon_name = mon($mon); $year += 1900; print "Today is $day_name, $mon_name $mday, $year.\n"; 

A.14.2. Exercise 2

Most of this answer is the same as the previous answer. We just need to add the parts for the export tag all.

 our @EXPORT = qw(day mon); our %EXPORT_TAGS = ( all => \@EXPORT ); 

Everything that we put in %EXPORT_TAGS has to also be in either @EXPORT or @EXPORT_OK. For the all tag, we use a reference to @EXPORT directly. If we don't like that, we can make a fresh copy so the two do not reference each other.

 our @EXPORT = qw(day mon); our %EXPORT_TAGS = ( all => [ @EXPORT ] ); 

Modify the program from the previous exercise to use the import tag by prefacing it with a colon in the import list.

The main program now starts off like this:

 use strict; use Oogaboogoo::date qw(:all); 




Intermediate Perl
Intermediate Perl
ISBN: 0596102062
EAN: 2147483647
Year: N/A
Pages: 238

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net