Sunday, February 3, 2008

Perl Scripts

Writing Perl scripts are a fun and easy way to automate complex processes.

This simple script downloads text files over network using HTTP protocol and could be used to automate test processes for web applications.

#!/usr/bin/perl
use IO::Socket;

die "Syntax: wget.pl uri\n" unless @ARGV > 0;

($schema,$host, $path) = split("[://]+", $ARGV[0], 3);

$sock = new IO::Socket::INET (PeerAddr => $host,
PeerPort => '80',
Proto => 'tcp');

die "$host is down\n" unless $sock;

print $sock "GET /$path HTTP/1.1\n";
print $sock "Host: ", $host, "\n";
print $sock "Connection: ", "close\n\n";

while ($data = <$sock>) {
  print $data;
}