PREVIOUS  TABLE OF CONTENTS 

The Perl Journal One-Liners

TPJ One-Liner #44

Asteroid 2000 BF19 was thought to be on a potentially dangerous approach path for us Terrans, with a possible impact in 2022. A Perl program called clomon.pl showed that the asteroid cannot come any closer than 0.038 AU for the next fifty years. Sleep tight!

óBased on email from Andrea Milani and Scott Manley

TPJ One-Liner #45

Tracking the progress of a file as it downloads:

perl -e 'BEGIN{$|=1;$f=$ARGV[0];$s=(stat$f)[7];$t=time}
	    while(sleep 1){printf"\r$f %s bytes at %.2f Kb/s   ",
        $_=(stat$f)[7],($_-$s)/1024/(time-$t)}' 

your_downloading_file

óCourtesy Philippe Bruhat

TPJ One-Liner #46

A full list of installed (but nonstandard) modules, and where they are located:

#!/usr/bin/perl -w
use strict;		  # all variables must be declared
use Getopt::Std;            # import the getopts method
use ExtUtils::Installed;    # import the package

use vars qw($opt_l $opt_s); # declaring the two option switches
&getopts('ls');             # $opt_l and $opt_s are set to 1 or 0
unless($opt_l or $opt_s) {  # unless one switch is true (1)
  die "pmods: A utility to list all installed (nonstandard) modules\n",
      "  Usage: pmods.pl -l # list each module and all its directories\n",
      "        pmods.pl -s  # list just the module names\n";
}

my $inst  = ExtUtils::Installed->new();
foreach my $mod ( $inst->modules() ) { # foreach of the installed modules
  my $ver = $inst->version($mod);      # version number of the module
     $ver = ($ver) ? $ver : 'NONE';    # for clean operation
  print "MODULE: $mod version $ver\n"; # print module names
  map { print "  $_\n" } $inst->directories($mod) if($opt_l);
}

óCourtesy William H. Asquith et al.

TPJ One-Liner #47

Print a message if a daylight savings time change occurs within the next 5 days:

print "\aTIME CHANGE COMING!\n"
   if (localtime(time))[8] ne (localtime(time+5*24*60*60))[8];

óCourtesy J.D. Laub

TPJ One-Liner #48

Reverse for syntax to print out Perl's include path:

perl -e 'print "$_\n" for @INC'

TPJ One-Liner #49

You can create a reference to a scalar like so:

   $ref = \$var;

In recent versions of Perl, you can also say:

   $ref = *var{SCALAR};

The same holds for other data types.

TPJ One-Liner #50

Upcoming in Perl 5.6:

A new keyword, our, which is like my but is package-aware.

TPJ One-Liner #51

$^O contains the name of your operating system.

TPJ One-Liner #52

The Data::Dumper module, bundled with Perl, can save data structures to disk as strings that can be read in by another program.


PREVIOUS  TABLE OF CONTENTS