≡ Menu

Perl for Beginners at BarCamp Delhi

Yesterday at BarCamp Delhi Prateek and Pratul decided to hold a hack-a-thon. The idea being, that in 2 to 3 hours, the group of programmers assembled would hack together some application. Some of the ideas thrown around were:

  • A Firefox Plugin
  • A Firefox Greasemonkey Extension
  • A manufacturing, logistical support web app

Pratul and Prateek probably have a more complete list than I do. The hack-a-thon broke apart into a few groups, each doing something different. No applications were built but a few Windows programmers were interested enough that I spent about 20 minutes showing them a few “Hello World” Perl scripts.

I’ve attached the Perl scripts for the folks who asked for them and any others that might be interested. Also, if anyone is interested in learning Perl, I highly recommend this book. It says 21 days to learn Perl but most of you will probably pick it up in less than a week.

Unfortunately, the extensions of the files are being changed to .txt so please take off the .txt once you download the files. I’m also putting the scripts inline.

hello.pl: A simple Hello World script


#! /usr/bin/perl

use warnings;
use strict;

my $name = "Pankaj";
print "Hello $name.n";


hello2.pl: A “Hello World” script that uses a very simple Perl module (below)


#! /usr/bin/perl

use warnings;
use strict;

use lib("../lib");
use Hello;

my $name = "Pankaj";
Hello::hello($name);


Please put the Hello.pm in a relative path to where you put hello2.pl as described in “use lib” line above in hello2.pl.

Hello.pm Our very simplistic Perl module


package Hello;

sub new {
}

sub hello{
my $name = $_[0];
print "hello " . $name;
} #
# ------------------------------------------------------------
1;


And our final example that one of the campers asked for was a Perl script that would take input from the command line interactively.

hello3.pl: Our final “Hello World” script that interactively asks for names on the command line.


#! /usr/bin/perl

use warnings;
use strict;
use Getopt::Long;
use Term::ReadLine;

my $foo;
my @bar;

GetOptions("foo=i" => $foo);

my $count = 0;
my $term = Term::ReadLine->new();

while ($count readline("What is your name? ");
$bar[$count] = $line;
$count++;
}

foreach my $f (@bar) {

print "bar is $fn";
}


We didn’t get to it but would one of the campers like to take one of these scripts and turn them into a CGI script or a mod_perl application? Please post your changes in a comment to this post.
Cheers

Related Posts:

Comments on this entry are closed.