O'Reilly logo

Learning Perl Student Workbook, 2nd Edition by brian d foy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Appendix P. Answers to Chapter 16 Exercises

Answer 16.1: Set the environment variable TZ by replacing its value in the %ENV hash. The value you need to set varies from system to system and, at least on a Unix machine, you use the names of files in /usr/share/zoneinfo. For the Pacific time zone, some systems use PST8PDT. After that, use exec to turn your perl process into the date command, which has the same environment as the perl process. You’ve effectively created a wrapper that sets up the environment then executes the desired command:

#!/usr/bin/perl
use strict;
use warnings;

$ENV{TZ} = 'PST8PDT';

exec 'date';

On a Mac OS X system, you have to use US/Pacific:

#!/usr/bin/perl
use strict;
use warnings;

$ENV{TZ} = 'US/Pacific';

exec 'date';

You might try this with a time zone that doesn’t exist. On some systems, you end up with GMT: the same time you get without a time zone.

Answer 16.2: When you use the backticks in a list context, you get a list of lines. Go through these lines with foreach. For the ls on my system, the first line is a header and starts with “total”, so skip that one. You might need to do something different. You can split the line on whitespace, the default separator, and assign the list to your list of variables. The first column is the file permissions string, which you don’t care about so you assign it to undef, which is really just a placeholder. The next column is the number of links to that file, but you don’t need that either, so you assign it to undef, which ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required