Browsing TagPerl

Install Perl module in your home directory

To install a Perl module in your home directory is helpful for a couple of reasons. 1) It doesn’t require root permission. 2) It’s installed separately from the site-wide installed modules, hence can coexist with those modules and useful when trying a module that has different version with what’s already installed.


$ perl -MCPAN -e shell PREFIX=~/lib/perl LIB=~/lib/perl
cpan> install
cpan> quit

Assuming that you want it installed on the directory lib/perl on your home directory. Also, don’t forget to set PERL5LIB:

export PERL5LIB=~/lib/perl:$PERL5LIB

Base64 Encoding and Decoding

At one time, I needed to encode and decode strings in Base64 but I was on a very old Perl version that does not include the MIME::Base64 core module, nor am I able to install the said module. So, here’s the source for encoding and decoding Base64 ripped from the MIME::Base64 module:

sub EncodeBase64
{
    my $s = shift ;
    my $r = '';
    while( $s =~ /(.{1,45})/gs ){
        chop( $r .= substr(pack("u",$1),1) );
    }
    my $pad=(3-length($s)%3)%3;
    $r =~ tr|` -_|AA-Za-z0-9+/|; 
    $r=~s/.{$pad}$/"="x$pad/e if $pad; 
    $r=~s/(.{1,72})/$1n/g; 
    $r; 
} 
 
sub DecodeBase64 
{ 
    my $d = shift; 
    $d =~ tr!A-Za-z0-9+/!!cd; 
    $d =~ s/=+$//; 
    $d =~ tr!A-Za-z0-9+/! -_!; 
    my $r = ''; 
    while( $d =~ /(.{1,60})/gs ){ 
        my $len = chr(32 + length($1)*3/4); 
        $r .= unpack("u", $len . $1 ); 
    } 
    $r; 
}

Uninstall Perl Module

Here’s how to cleanly uninstall any Perl module:

#!/usr/local/bin/perl

use ExtUtils::Packlist;
use ExtUtils::Installed;

$ARGV[0] or die "Usage: $0 Module::Namen";

my $mod = $ARGV[0];

my $inst = ExtUtils::Installed->new();

foreach my $item (sort($inst->files($mod))) {
         print "removing $itemn";
         unlink $item;
}

my $packfile = $inst->packlist($mod)->packlist_file();
print "removing $packfilen";
unlink $packfile;

Sorting IP Addresses

The following will sort an array of IP addresses in @in and the sorted IP addresses will be in @out.

@out = sort {
    pack('C4' => $a =~ /(d+).(d+).(d+).(d+)/)
    cmp
    pack('C4' => $b =~ /(d+).(d+).(d+).(d+)/)
} @in;

What this does is it forms a string of four bytes out the IP address octet using the pack() function then sorts it lexicographically.

See also Sorting Section Numbers

Write a Daemon in Perl

The code below is a template for a daemon written in Perl. Use the code below as a starting point when you have to write a program that has to persist in the background to do its things and without a gui.


use POSIX qw(setsid);

chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null'
    or die "Can't read /dev/null: $!";

#open STDOUT, '>/dev/null'
#    or die "Can't write to /dev/null: $!";

open STDERR, '>/dev/null'
    or die "Can't write to /dev/null: $!";

defined(my $pid = fork)
    or die "Can't fork: $!";

exit if $pid;

setsid or die "Can't start a new session: $!";

while(1) {
    sleep(5);
    print "Hello...n";
}

Note that one of the lines above is commented out to let the output print to the screen. Uncomment this in the final code to silence your program. For more on this code, see this tutorial: http://www.webreference.com/perl/tutorial/9/.