[Ilugc] [TIP] perl tutorial VII (perl tricks)

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Fri, 9 Dec 2011 08:24:31 +0530

Perl is a language full of tricks because it is nothing but UNIX command line
tools with some innovations of its own like regex extensions, easy and
 well integrated OO to solve complex problems easily, easy file I/O, easy
 socket I/O, easy IPC facilities in UNIX and so on.

However using pack(), vec(), map() and so on is hard.

I have not yet understood how map() works. It is a functional
programming concept.

pack() is used for binary protocols and for encoding in various forms.

In a lot of ways perl is messy and with a syntax that is so odd that
it is often called

"The write once forget language"

In other words perl is used for quick and dirty jobs.

But that is not strictly true. Perl is also used a lot in serious
tasks and many UNIX utilities
 are built with perl. The entire package management in OpenBSD which
is the best package management tool
in the UNIX world developed by Marc Espie, a French gentleman is in
perl using perl OO.

Perl is slow and not really as fast as C. It can never be faster than C.

In fact C is a middle level language which acts as a low level
language in the kernel(particularly bootup startup code)
 and as a high level language when used for GUI like GTK and as  a
middle level language in most applications
like networking, I/O and so on.

Perl on the other hand is a high level language. It is a scripting
language; which means that it is a
 language used to express your ideas in a language that looks like English.

This could be said of python but perl is not even half as elegant.

It is only its integration with UNIX ideas, its superb ability to copy
great ideas and integrate well that it
 is such a hit amongst UNIX geeks like me.

Why was I not forced to learn Python deeply? But perl is something I
use everywhere. This should give you a hint.

Anyway let us get down to business.

die "The program does not have any sense!\n" unless(/something important/);

This is an example to prove that perhaps perl is like English and a script.

What is a script?

A script is written by hand or a poem or Haiku or a scrawl.

Perl can be used that way. It can be used without much thought about
efficiency or variable scoping, typing
 or declaring.

All perl variables are by default global. By global people say file
scope, package scope and so on.

Anyway let us not get into that.

Let us see another example.

$ perl i.pl /etc/passwd
88ced295c57a727aadab4e626dad47b1df6c98e3
$ sha1 /etc/passwd
SHA1 (/etc/passwd) = 88ced295c57a727aadab4e626dad47b1df6c98e3
$ cat i.pl
use Digest::SHA1 qw(sha1_hex);

$digest = sha1_hex(<>);

print "$digest\n";

sha1 is a command in OpenBSD. On Linux you say sha1_sum which a GNU thing.

Anyway there are plenty of tricks one could do with the UNIX in perl.

Look at this:

$ cat i.pl
open PIPE, "netstat -a|";

while(<PIPE>) {
        print;
}

Here what we do is open a pipe to the netstat -a command. It lists all
the sockets in the system.

We can of course do this:

$ perl i.pl
       2       8      37
$ cat i.pl
open PIPE, "|wc";

print PIPE "What are you doing?\n";

print PIPE "Let me count now\n";

close PIPE;

-Girish


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

Other related posts: