web analytics

Perl: print the name of the input file

The special variable $ARGV could be used to get the name of the input file. It returns the name of the file or ‘-‘ if the standard input was used.

Here are some examples:

perl -nle ‘END { print $ARGV }’ /etc/passwd /etc/passwd echo test | perl -nle ‘print $ARGV’ – $ (echo test; echo […]

Perl: how to get the position of the match

Let’s search for the word “brave” in the string “Hello, new brave world!”. There are several methods to get the position of the match in Perl.

#1 – use POS function Function pos returns the position, where search was finished.

$txt = "Hello, brave new world!" ; $txt =~ m/(brave)/g; my $pos = pos($txt) – […]

Perl: SOAP problem with dateTime

The following error message was received by Perl application: Unrecognized type ‘{http://www.w3.org/1999/XMLSchema}dateTime

The problem is caused by “dateTime” type: it was not supported in the 1999 XML Specification. The solution is to switch to 2001 schema:

 my $soap = SOAP::Lite->uri( $NAMESPACE )->proxy( $PROXY_URL )->xmlschema (’2001’);

Perl: Soap deserialization error

Sometimes the following error is displayed, if application uses the old version of CPAN SOAP module: “Client Application failed during request deserialization: no element found at line 1, column 0, byte -1 at XML/Parser.pm”

The possilbe solution is to change SOAP::Transport::HTTP module (SOAP/Transport/HTTP.pm), function “handle”:

sysread( STDIN, $buffer, $length )

should be replaced by

read( […]

Perl: get file checksum

use Digest::MD5; use IO::File; my $chk = Digest::MD5->new(); foreach my $file (@ARGV) {     $chk->addfile(IO::File->new($file));     print "$file -> ",$chk->hexdigest,"\n"; }

The additional details could be found on IBM site

Unix: get the file date

ls -1 | cpio -o | cpio -ivt | awk ‘{print $NF, $(NF-1), $(NF-4), $(NF-3) }’

Warning: I/O expensive for the large files!


@a = localtime((stat($my_file))[9]); $a[4]++; printf "%02d%02d%02d",@a[5,4,3];

Calculating the date of Easter

The easiest way is to use specialized package (like Date::Calc). If You need standalone function, the following method could be used.

#!/usr/bin/perl $year=$ARGV[ 0 ]; $isJulian=(defined($ARGV[ 1 ]) && $ARGV[ 1 ] eq ‘julian’); $isOrthodox=(defined($ARGV[ 1 ]) && $ARGV[ 1 ] eq ‘orthodox’); my ( $G , $C , $H, $I, $J , $L ); […]