[Ilugc] [TIP] perl tutorial I

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Sat, 3 Dec 2011 06:52:11 +0530

Yesterday we saw some perl code/opinions and history of perl.

Today we will see more.

As I said perl grew up in the UNIX world with UNIX command line tools
and shell scripting
 ideas. You can even look at it as a shell scripting language on steroids.

As we go along you realize that if you know UNIX well already you are
well on way towards
 becoming a perl expert.

grep,tr,sort everything is also there in perl as functions.

$perldoc -f grep

And you can easily call shell executables using

$output = `grep girish /etc/passwd`;

system("rdate time-a.nist.gov");

or

exec("cp /etc/passwd /tmp");

Though you can have OS bindings and interactions with any language,
scripting or otherwise with perl
 the UNIX ideas are all over.

You can open a UNIX pipe just like you open a file.

Perl's modules provide you with functions that the shell commands do
without doing a fork(), exec() and
 calling  a shell executable which is very costly.

And contrary to what people think and believe perl only slightly
improved the regular expressions already
 used in the UNIX world in shell globbing and so on.

Look at man flex. You find nearly all the perl regex ideas.

And perl code is not really intuitive. It has its defects along with
its strengths.

But just like we have an inferior language like English which cannot
match up to Tamil whichever way you
 look at it, perl though not as great as others is popular, successful
and enjoys support.

So I use it , I spent time learning it and continue to use it.

Let us get back to explaining the foreach() array iterator from yesterday.

Before that few general comments about scripting and high level languages.

High level languages all consider the default type of a variable as a
string. Of course you have to
 quote them, but you don't have strong typing as in C.

In fact it is the weak typing of variables in which they can become
integers or strings depending on the
 context that make scripting languages so easy to deal with.

Scripting languages also make several difficult things easy. Due to
perl's strong UNIX affiliation you can
 do a lot of things you have been doing in the UNIX world using pipes
and various command line tools more
efficiently in a better programming language like perl.

And network programming and many cool things that C does perl can too.
Without the difficulty and pain.

It has object orientation and is a very good OO language unlike C++ or
Java. Perl give many choices and
 as I said, in typical UNIX style, it is very well documented, it has
accurate example code in the man pages
 and the CPAN modules/third party libraries are of very high quality.

Overall we get a very pleasant experience with perl.

As part of these multifaceted conveniences and powers, we also have
what is called as array iterators.

This is also found in shell scripts and certainly in languages like
Python which has advanced forms of the same idea.

For instance, instead of using for() loops like in C for iterating
thro' array elements, we say

foreach $var (1,2,4,5,100) {
         print $var . "\n";
}

Or this:

for $var (1..100) {
        print $var . "\n";
}

It is very similar to the shell scripting concept:

for var in `cat /tmp/file`
do
         echo $var
done

See?

These are called iterators.

We also see two more of perl's superb features.

One is the range operator. 1..20 to print numbers from 1 to 20. Cool.

You can use them anywhere we want.

And the string concatenation operator ".".

$o = "first " . "second" . "third";

will simply add the three strings(strcat() in C).

Then you have another great feature.

$foo = "imposition " x 40;

will write the string "imposition" for you 40 times.

Okay we have seen enough explanation and English till now.

From tomorrow we focus only on code samples.

I will illustrate whatever I have been trying to say in English so far.

-Girish

Other related posts: