Archive

Archive for the ‘perl’ Category

Perl study note 2: using unpack() to parse formatted data

February 17, 2011 Leave a comment

Example code: formatting 10-digit us phone numbers
#!/usr/bin/perl
# just for the purpose of illustrating the usage of unpack() function
# of course there are other ways such as regex, substring to get the
# same result
use strict;
use warnings;

my @numbers=(
'2223334444',
'2223456789',
'2228889999',
);

for my $n (@numbers) {
my ($area, $phone1, $phone2)=unpack('a3a3a4', $n);
print "$n => ($area)$phone1-$phone2\n";
}

Result:
2223334444 => (222)333-4444
2223456789 => (222)345-6789
2228889999 => (222)888-9999

This might not be the best solution to get this kind of task done but definitely it’s neater than using the substring method.

Advertisements
Categories: perl, Programming

Perl DBI atomic fetch

February 17, 2011 Leave a comment

When getting a single row of data from a database table is desired, using atomic fetch makes life a lot easier, for example (assuming $dbh connection is established)
Example 1:

my ($name, $country) = $dbh->selectrow_array( "SELECT name, country FROM tbl_customer" );
print "Customer $name is from $country.\n";

Example 2:

my ($cnt) = $dbh->selectrow_array( "select count(*) from tbl_customer");
print "There are $cnt record(s).\n";

Reference:
http://docstore.mik.ua/orelly/linux/dbi/ch05_06.htm

Categories: perl, Programming

check host is alive or not with perl Net::Ping

December 26, 2009 9 comments

With Bash

#!/usr/bin/bash
chost() {
    up=`ping -c 1 -W $2 $1|grep 100%`
    return `test -z "$up"`
}
[ $# -lt 1 ] && "Usage is: $0 host [timeout=5]" && exit 1
[ $# -gt 1 ] && timeout=$2 || timeout=5
chost $1 $timeout && echo "$1 is alive"

Problem with this script:
If the default route is missing when running this script to test an external host, the result will show the host is alive, along with the following ping error:
connect: Network is unreachable

Hence the (improved) version of ping script with Perl:

With Perl

#!/usr/bin/perl -w
use Net::Ping;
use Switch;

if (@ARGV >1) { $host=$ARGV[0]; $timeout=$ARGV[1]; }
elsif (@ARGV>0) { $host=$ARGV[0]; $timeout=5; }
else { print "Usage is: $0 host <timeout=5>\n"; exit; }

$p=Net::Ping->new('icmp');
print "$host is alive.\n" if $p->ping($host, $timeout);
$p->close();

Embed perl in bash

I found it quite handy to use the above perl script in some of my gateway management bash scripts.

#!/bin/bash
chost() {
export host=$1
export timeout=$2
a=`perl -MNet::Ping -e '$p=Net::Ping->new('icmp'); \
   if( $p->ping($ENV{'host'},$ENV{'timeout'}) ) \
           {print "OK";} \
   else {print "BAD";} \ 
   $p->close();'`
return `test $a == "OK"`
}
[ $# -lt 2 ] && echo "Usage is: $0 host timeout" && exit 0
chost $1 $2 && echo "good" || echo "not good"

Extract ip address(es) from standard input using perl and regular expression

December 15, 2009 3 comments

Method 1:

$ cat file | perl -nle 'print "$1" if
/(?!0+\.0+\.0+\.0+$)(([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5]))/'


Method 2:

$ cat file | perl -nle 'print $1 if /(([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+))/g && $2<256 && $3<256 && $4<256 && $5<256'

I prefer using method 2 because it’s simpler to construct and for ip addresses such as 1.2.3.1234 (obvious an invalid one), using method 1 results in1.2.3.123 being matched. But method 2 will not mistakingly pick up that ip address.

Reference: Mastering Regular Expressions by Jeffrey E.F. Friedl.

Categories: perl, Programming