Tue, 09 Jun 2009

Small Perl Helpers


Permanent link

CoreList

You want to use a module, but you're only allowed to use core modules? Or you want to recommend a module to somebody, and you know it's more likely that he'll use a module if it's in core (and thus he won't hand-roll his crappy CGI parser, and open a wide door to spammers)?

Module::CoreList is the answer, and it comes with a very handy script called corelist:

$ corelist Unicode::Normalize
Unicode::Normalize was first released with perl 5.007003

$ corelist DBI
DBI was not in CORE (or so I think)

# search with regexes
$ corelist /Tie/
Pod::Simple::TiedOutFH was first released with perl 5.009003
Tie::Array was first released with perl 5.005
Tie::File was first released with perl 5.007003
Tie::Handle was first released with perl 5.00405
Tie::Hash was first released with perl 5.002
Tie::Hash::NamedCapture was first released with perl 5.009005
Tie::Memoize was first released with perl 5.007003
Tie::RefHash was first released with perl 5.004
Tie::Scalar was first released with perl 5.002
Tie::StdHandle was first released with perl 5.01
Tie::SubstrHash was first released with perl 5.002
TieHash was first released with perl 5

(empty lines sanitized; corelist emits an empty line after each module which is a bit annoying)

Timestamps

Not from CPAN, but a tiny script I wrote:

#!/usr/bin/perl
use strict;
use warnings;
use Time::Local qw(timelocal);


if (@ARGV) {
    my $date = shift @ARGV;
    if ($date =~ m/^(\d{4})-(\d\d)-(\d\d)$/){
        my ($year, $month, $mday) = ($1, $2, $3);
        my ($hr, $min, $sec) = (0, 0, 0);
        my $time;
        if ($time = shift(@ARGV)
            and $time =~ m/^(\d{1,2}):(\d\d)(?::(\d\d))?$/) {

            ($hr, $min, $sec) = ($1, $2, $3||0);
        }
        print timelocal($sec, $min, $hr, $mday, $month - 1, $year - 1900), $/;
    }

} else {
    print time, $/;
}

If called with no argument it will print the current time as a Unix timestamp. If called with one or two arguments, it will interpret these as a date in the YYYY-MM-DD and a time in HH:MM:SS format and print the corresponding timestamp.

[/perl-tips] Permanent link

comments / trackbacks