Using perl to convert short urls (shortened urls) into long urls

Perl is a powerful and useful programming language. It has been largely popular for it’s string manipulation, speed and ease of using. Perl is one of my favourite languages of choice. It suits best for small programs which needs to one thing but do it best.

When I was writing a software to analyse twitter statuses, i had to see what people were actually trying to say. In twitter culture people include urls to present their view. For example, to say you don’t like Obama’s new agenda, you can give a link to a url where the view is strongly presented. Thus, to abstract the sentiment of tweet by a tweeter user, I had to look for url present in their tweets and rank their sentiments. Also, due to the limitation of tweet being 140 character, people include shortened urls in order to save spaces. These short urls use services like, tinyurl, etc.

Thus to lengthened the shortened urls, i built my own perl code. I made a sub function where I will pass a short url and it will return a long url for me. This will mean that when I come across a long url in a tweet I can immediately get a long url of it and get the rank of the site after seeing it (manually).

The libraries and modules that I used is WWW::Curl::Easy and XML::QL. This script basically uses the api of The result which is returned from the site is what we need after we do some small formatting. WWW::Curl::Easy is the backend to pull the page. By using the XML::QL module we can actually find the location from the xml data which comes by when the curl function returns the result.

The Code:

use WWW::Curl::Easy;
use XML::QL;


sub long_url
my $url = shift;

$url =~ s/:/%3A/gi;
$url =~ s/\//%2F/gi;
$url = ‘‘ . $url;

my $curl = new Www::Curl::Easy;
$curl->setopt(CURLOPT_URL, $url);
my $response_body;

open(my $fileb, “>” , \$response_body);

my $retcode = $curl->perform;
my $result;

if ($retcode == 0) {
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
open (FL, “>tmp_curl”);
print FL $response_body;
close FL;

my ($qry,$long_url);
$qry = ‘WHERE <long-url>$long_url</long-url> ORDER-BY $long_url IN “file:tmp_curl” CONSTRUCT $long_url’;
$result = XML::QL->query($qry);
return $result;
} else {
$result = print(“An error happened: ” . $curl->strerror($retcode).” ($retcode)\n”);

And According to Mr. Laryy Wall the following can be a small alternative to the above:
perl -MLWP::UserAgent -e ‘print LWP::UserAgent->new->get(””)->base’