Archive for the ‘Perl & CGI’ category

Character matching in perl

January 24th, 2011

With respect to character matching, there are a few more points you need to know about. First of all, not all characters can be used ‘as  is’ in a match. Some characters, called metacharacters, are
reserved for use in regexp notation. The metacharacters are

{}[]()^$.|*+?\

The significance of each of these will be explained in the rest of the tutorial, but for now, it is important only to know that a metacharacter can be matched by putting a backslash before it:

“2+2=4″ =~ /2+2/; # doesn’t match, + is a metacharacter
“2+2=4″ =~ /2\+2/; # matches, \+ is treated like an ordinary +

“The interval is [0,1).” =~ /[0,1)./ # is a syntax error!
“The interval is [0,1).” =~ /\[0,1\)\./ # matches
“#!/usr/bin/perl” =~ /#!\/usr\/bin\/perl/; # matches

Thanks
Manoj Chauhan

Sed Examples

December 15th, 2010

Use sed recursively

Apply a sed command to all files found recursively on target directory (xargs allows breaking the list of arguments into smaller ones when dealing with large number of files) find </path/to/dir> -type f | xargs sed -i ‘s/<replaceThis>/<withThis>/’

It seach inside on all files of /tmp folder for Installing and if found it will replace it with manoj-Installing
find /tmp/ -type f | xargs sed -i ‘s/Installing/manoj-Installing/’

Apply a sed command only to certain files (for instance all css files)
find </path/to/dir/*.css> -type f | xargs sed -i ‘s/<replaceThis>/<withThis>/’

Apply a sed command only to files that contain a certain pattern
grep -rl “<pattern>” </path/to/dir> | xargs sed -i ‘s/<replaceThis>/<withThis>/’

White spaces

Remove/delete leading whitespaces (tabs,spaces) from each line and align to left
sed ‘s/^[ \t]*//’

Remove/delete trailing whitespaces (tabs,spaces) from each line
sed ‘s/[ \t]*$//’

Remove/delete leading and trailing whitespaces from each line
sed ‘s/^[ \t]*//;s/[ \t]*$//’

Undefined subroutine &main::param called

April 29th, 2010

I am getting this error (Undefined subroutine &main::param called) in /var/log/httpd/error_log

Basically i am not getting any value after posting

if ( $ENV{REQUEST_METHOD} eq “POST” )
{
my %form;
foreach my $key (param()) {
$form{$key} = param($key);
print “$key = $form{$key}<br>\n”;
}
}

I have resolved it by changing use CGI; to CGI qw(:standard); in the top of the script

Cheers!!!
Manoj Chauhan