10
Aug 09

Check Google Page Rank with Perl

If you are bored with stupid online scripts or browser plugins for checking the almighty Google Page Rank, here is a simple script in Perl to do it:

#!/usr/bin/perl

use WWW::Google::PageRank;

{

print 'Enter a domain: ';

chomp(my $domain = <STDIN>);

last if $domain eq 'q';

my $pr = WWW::Google::PageRank->new;

print 'PR: '.scalar($pr->get("http://$domain")), "\n";

redo;

}

Needless to say, I have used my favorite Perl loop :) This way you can check as many domains as you'd like. Of course you will need to install the perl module WWW::Google::PageRank first. For this purpose it might be a good idea to use cpan because there are a few dependencies and it will take time to install them manually.

9
Aug 09

Perl and Map Line Insertion

Perl's map function is quite powerful and even its simplest usage is fascinating. Once I have read about it I came up with a handy solution - how to insert text at the beginning of every line.

This can be done with just 1 line in perl:

print map { "YOUR TEXT  $_" } (<>);

In case something is unclear explanation follows:

YOUR TEXT will be the text printed at the beginning of every line.

Perl's default variable $_ represents the original line, in our case from a text file.

(<>) - the diamond operation assumes you will be reading a file.

So a sample, minimal perl, Linux usage will be:

$perl -e 'print map { "YOUR TEXT  $_" } (<>)' OLD_FILE.txt > NEW_FILE.txt

This will insert YOUR_TEXT at the beginning of each line from the OLD_FILE.txt and save it all to NEW_FILE.txt.