[Ilugc] [TIP] perl beginning

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Thu, 1 Dec 2011 10:19:47 +0530

Dear all,

I have intermittent Internet access. Hence the absence of tips on the days
 I am unavailable.

I shall try to fix this problem.

Since we have seen a little bit of javascript, jQuery and CGI I think
we should revisit this
later. It is not possible to learn everything at one go.

Today I am starting perl training. Since I am not very good at Python
I will send tips in python later
 to improve my skills in that. ;)

Anyway let us begin with some history and background of perl.

This will also put you on a good footing to attend my perl class on
the 18th of this month at TalentSprint
 Royappetah.

Simply put Perl is a scripting language, an interpreted language and
it began with a  focus on CGI and
text processing in the UNIX world.

Today we find perl being used in all OSes but it is primarily a UNIX
thing and has very strong leanings towards
 UNIX geeks, IPC and UNIX internals than Python.

Is this is the reason people in India prefer Python over perl? I dunno.

I cannot answer that.

At least in LUG, the perl knowledge seems woefully poor.

Anyway perl is an interpreted language. Which means that the perl
source and executable are the same.

But that does not mean that we cannot compile perl.

$ perl -c first.pl

will tell you whether the compilation phase of perl is successful.
This is very nice.

We can identify a lot of bugs that way. Also the

$ perl -w

switch and the

use strict;

in the perl source code helps us debug further.

Now let us look at a hello world.

#!/usr/bin/perl

print "Hello world\n";

$ perl -c first.pl

$ perl first.pl

will execute it and print it.

Now, perl has mainly 3 data types, scalars, arrays and associative
arrays/dictionary/hash.

Unlike shell scripts and makefile variables, perl always requires a
variable to be prefixed with one of
 $, @ or # before it.

Even when assigning a value.

For example, in shell we say,

Var=1
echo $Var

But in perl we say

$var=1;
print $var;

And remember every line in perl needs to have a  semicolon.

And though it looks like C, you cannot have an if condition without a {}.

However perl does allow this;

print "no match" if($var ne 10);

This looks like English. Perl has this construct.

But you cannot do this;

if($var ne 10)
           print "no match";

Even single line statements need to go inside curly braces.

So let us look at a slightly complicated example before calling it a day.

$cat second.pl
#!/usr/bin/perl -w
use strict;
my ($first, $second) = ();

$first = "something";
$second=2;

print "The value of first is $first\n";

print "The value of second is $second\n";


We will see more in due course. Stay tuned.

-Girish
-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts: