Section A.2. Answers for Chapter 3


A.2. Answers for Chapter 3

A.2.1. Exercise 1

The trick in this exercise is to let the modules do all of the hard work. It's a good thing we've shown you how to use modules! The Cwd module (cwd is an acronym for "current working directory") automatically imports the getcwd function. We don't have to worry about how it does its magic, but we can be confident that it does it correctly for most major platforms.

Once we have the current path in $cwd, we can use that as the first argument to the catfile method from the File::Spec function. The second argument comes from the input list to our map and shows up in $_.

 #!/usr/bin/perl use Cwd; use File::Spec; my $cwd = getcwd; print map { "    " . File::Spec->catfile( $cwd, $_ ) . "\n" }                 glob( ".* *" ); 

A.2.2. Exercise 2

We can't give you much help installing the module, although if you run into problems, you might want to ask brian about them since he wrote the module, as well as the cpan program you can use to install it.

Once we have the module, we just have to follow the example in the documentation. Our program takes the ISBN from the command line and creates the new ISBN object, which we store in $isbn. Once we have the object, we simply follow the examples in the documentation.

 #!/usr/bin/perl use Business::ISBN; my $isbn = Business::ISBN->new( $ARGV[0] ); print "ISBN is " . $isbn->as_string . "\n"; print "Country code:   " . $isbn->country_code . "\n"; print "Publisher code: " . $isbn->publisher_code . "\n"; 




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