PERL

Perl Script to check Google pagerank

2010
11
May

In order to find out a Google Pagerank of a webpage, we can use the WWW::Google::PageRank module in Perl.
Click WWW::Google::PageRank to download the Google Pagerank module. Here is a simple script that takes the Website URL as a command line argument and prints the Google PageRank.

#!/usr/bin/perl
 
$webpage = $ARGV[0];
 
use WWW::Google::PageRank;
my $pagerank = WWW::Google::PageRank->new;
print scalar($pagerank->get($webpage)), "\n";



PERL: How to remove an element from hash or array?

2010
29
Mar

In order to remove an element from the array or hash, we have to use the delete keyword on perl. Using the delete keyword on the key of the hash or array (numeric keyed hash), we can delete elements of a hash or array.

Example for deleting an element from a hash

The following code deletes the key 'a' from the hash

%hash

.

#!/usr/bin/perl
$, = "\n"; # Fields separator
my %hash = (
'a' => 'aaa', 
'b' => 'bbb',
'c' => 'ccc',
);
 
print %hash;
delete $hash{a}; #key value pair 'a' => 'aaa' would be deleted
print %hash;

Read More

Resolve Net::MySQL Hang problem if the database result has only 1 row or is empty

2010
26
Mar

Net::MySQL is a simple MySQL perl Client to connect to MySQL servers. But Net::MySQL hangs if your result set is having just one row.

Sample Net::MySQL code

#!/usr/bin/perl
 
use Net::MySQL;
 
$mysql = Net::MySQL->new(
	hostname => 'localhost',
	database => 'db',
	user     => 'user',
	password => 'password',
);
 
$gid = 1;
 
$mysql->query('SELECT * FROM table WHERE id = 1');
 
if ($mysql->has_selected_record) {
	my $record_set = $mysql->create_record_iterator;
	while(my $record = $record_set->each) {

Read More

Error Installing PERL modules "unrecognized command line option -fstack -protector"

2010
24
Mar
4

unrecognized command line option "-fstack-protector" error usually comes on Cygwin when installing PERL modules.
For example when installing the DBI module or DBD::MySQL perl module, the following error comes on cygwin :

cp lib/DBI/Gofer/Serializer/Base.pm blib/lib/DBI/Gofer/Serializer/Base.pm
cp dbipport.h blib/arch/auto/DBI/dbipport.h
cp lib/DBI/Gofer/Execute.pm blib/lib/DBI/Gofer/Execute.pm
cp lib/DBI/DBD.pm blib/lib/DBI/DBD.pm
cp lib/Win32/DBIODBC.pm blib/lib/Win32/DBIODBC.pm
cp lib/DBI/PurePerl.pm blib/lib/DBI/PurePerl.pm

Read More

Make WWW::Mechanize do not exit on GET Errors

2010
02
Mar
2

When using PERL and WWW::Mechanize, your script can stop if there is some error in GETing a URL (such as 404 Not Found, 403 Permission Denied). Instead of resuming, the script terminates. If you want the script to continue resuming, you have to pass

autocheck => 0

in the Mechanize class constructor as shown below

my $mech = WWW::Mechanize->new(autocheck => 0);

Read More

Perl Script to Fetch PNR Status reservation on train from Indian Railways

2010
04
Feb

The code below is a perl script that fetches the PNR status from the indianrail.gov.in website and displays it on the command line.

Warning: No one is authorised to make any type of commercial usage like putting web advertisements or SMS service and Reproducing/Transmitting/Storing in its database, any content of www.indianrail.gov.in website, without prior written permission from Indian Railways. Violators will be severely prosecuted.

Read More

Perl Script to send Free SMS to any mobile number in INDIA using way2sms

2009
02
Dec
23

The following script sends SMS to any number in INDIA using the free service provided by Way2Sms.com
Requires WWW::Mechanize and Compress::Zlib libraries.

Please remember to fill the username and password in the fields that is mentioned in the script.

Updated code on 25th April 2010. If you face errors use the new script.

#!/usr/bin/perl
 
use WWW::Mechanize;
use Compress::Zlib;
 
my $mech = WWW::Mechanize->new();
 
my $username = ""; #fill in username here

Read More

Perl script to remove a directory and contents recursively

2009
26
Nov
2

This is the script for sandeep when he asked me how to delete a directory in perl. rmdir function only removes empty directories. So we need to remove the contents of the directory before removing the directory. So if the directory contains more directories / folders we would have to recursively delete all the directories under the directory. Well so here is the code just to do that.

Read More

Syndicate content