B.5. Some Important Modules
We describe some of the most important features[403] of the most important modules[404] in this section. These modules that we discuss
here should generally be found on every machine that has Perl, except
where mentioned. You can always get the latest ones from CPAN.
B.5.9. The POSIX Module
If you need access to the POSIX (IEEE Std 1003.1) functions, the
POSIX module is for you. It provides many
functions that C programmers may be used to, such as trigonometric
functions (asin, cosh), general
mathematical functions (floor,
frexp), character-identification functions
(isupper, isalpha), low-level
IO functions (creat, open), and
some others (asctime, clock).
You'll probably want to call each of these with its
"full" name; that is, with POSIX and a
pair of colons as a prefix to the function's name:
use POSIX;
print "Please enter a number: ";
chomp(my $str = <STDIN>);
$! = 0; # Clear out the error indicator
my($num, $leftover) = POSIX::strtod($str);
if ($str eq '') {
print "That string was empty!\n";
} elsif ($leftover) {
my $remainder = substr $str, -$leftover;
print "The string '$remainder' was left after the number $num.\n";
} elsif ($!) {
print "The conversion function complained: $!\n";
} else {
print "The seemingly-valid number was $num.\n";
}
B.5.12. The Time::Local Module
If you have a time (for example, from the
time function) that needs to be converted to a
list of year, month, day, hour, minute, and second values, you can do
that with Perl's built-in localtime function
in a list context.[406] (In
a scalar context, that gives a nicely formatted string representing
the time, which is more often what you'd want.) But if you need
to go in the other direction, you may use the
timelocal function from the
Time::Local module instead. It's important
to note that the value of $mon and
$year for January 2004 are not
1 and 2004 as you might expect,
so be sure to read the documentation before you use this module. For
example:
use Time::Local;
my $time = timelocal($sec, $min, $hr, $day, $mon, $year);
 |  |  | B.4. Extending Perl's Functionality |  | B.6. Pragmas |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|
|