Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [418]

By Root 1193 0
time string.

To use this report for the output, we replace lines 19 to 22 in the original script with the following:

$thetime = sprintf("%02d:%02d", $hours{$user}, $minutes{$user});

write;

The first line uses the sprintf function to format the time string and save it in the variable $thetime; the second line is a write command that tells Perl to go off and use the given report format to print a line of output.

Using this report format, we'll get something looking like this:

User Total login time Total logins

-------------- -------------------- -------------------

johnsonm 01:07 11

kibo 00:42 3

linus 98:50 208

mdw 153:03 290

Using other report formats, we can achieve different (and better-looking) results.

Perl comes with a huge number of modules that you can plug in to your programs for quick access to very powerful features. A popular online archive called CPAN (for Comprehensive Perl Archive Network) contains even more modules: net modules that let you send mail and carry on with other networking tasks, modules for dumping data and debugging, modules for manipulating dates and times, modules for math functions—the list could go on for pages.

If you hear of an interesting module, check first to see whether it's already loaded on your system. You can look at the directories where modules are located (probably under /usr/lib/perl5) or just try loading in the module and see if it works. Thus, the command

$ perl -MCGI -e 1

Can't locate CGI in @INC...

gives you the sad news that the CGI.pm module is not on your system. CGI.pm is popular enough to be included in the standard Perl distribution, and you can install it from there, but for many modules you will have to go to CPAN (and some don't make it into CPAN either). CPAN, which is maintained by Jarkko Hietaniemi and Andreas König, resides on dozens of mirror sites around the world because so many people want to download its modules. The easiest way to get onto CPAN is to visit http://www.perl.com/CPAN.

The following program—which we wanted to keep short, and therefore neglected to find a useful task to perform—shows two modules, one that manipulates dates and times in a sophisticated manner and another that sends mail. The disadvantage of using such powerful features is that a huge amount of code is loaded from them, making the runtime size of the program quite large:

#! /usr/local/bin/perl

# We will illustrate Date and Mail modules

use Date::Manip;

use Mail::Mailer;

# Illustration of Date::Manip module

if ( Date_IsWorkDay( "today", 1) ) {

# Today is a workday

$date = ParseDate( "today" );

}

else {

# Today is not a workday, so choose next workday

$date=DateCalc( "today", "+ 1 business day" );

}

# Convert date from compact string to readable string like "April 8"

$printable_date = UnixDate( $date, "%B %e" );

# Illustration of Mail::Mailer module

my ($to) = "the_person\@you_want_to.mail_to";

my ($from) = "owner_of_script\@system.name";

$mail = Mail::Mailer->new;

$mail->open(

{

From => $from,

To => $to,

Subject => "Automated reminder",

}

);

print $mail <<"MAIL_BODY";

If you are at work on or after

$printable_date,

you will get this mail.

MAIL_BODY

$mail->close;

# The mail has been sent! (Assuming there were no errors.)

The reason modules are so easy to use is that Perl added object-oriented features in Version 5. The Date module used in the previous example is not object-oriented, but the Mail module is. The $mail variable in the example is a Mailer object, and it makes mailing messages straightforward through methods such as new, open, and close.

To do some major task such as parsing HTML, just read in the proper CGI package and issue a new command to create the proper object—all the functions you need for parsing HTML will then be available.

If you want to give a graphical interface to your Perl script, you can use the Tk module, which originally was developed for use with the Tcl language; the Gtk module, which uses the newer GIMP Toolkit (GTK) ; or the Qt module, which uses the Qt toolkit

Return Main Page Previous Page Next Page

®Online Book Reader