LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Blogs > anestis89
User Name
Password

Notices


Rate this Entry

The beauty of Perl

Posted 07-27-2018 at 06:13 PM by anestis89

It's been some time from my first post. I have almost finished "Learning Perl",
2nd edition from Randal Schwartz and Tom Christiansen, and I have to say I was
amazed by this language. Now, this edition is rather old (1999) because I borrowed
it from my local library, today it's in the 7th edition, but the language didn't
change very much. It is still Perl 5.

A few words about Perl: Perl is an old language. It was created in 1987 by Larry
Wall. It was the standard language of the web two decades back. You can find more
in its website and Wikipedia. I want to mention just two things: Many of you know
that Perl is famous for its motto "TMTOWTDI" ("There's more than way to do it")
and that's true. Some find it a bad programming habit, other don't. Also, Perl
is known as a "write-only" language because we can write easily programs that
it's impossible to read. Personally, I like the Perl way. As for readability, I
think that depends on the programmer. Let's see a very simple Perl program:

Code:
#!/usr/bin/perl

use strict;
use warnings;

foreach (reverse <>) {
	print;
}
That's basically the UNIX tac program (without options) in Perl. Let's break
it down.

-> The first line defines the interpreter that the shell will use for executing
the script. The location can be found with the command 'which perl'.

-> The next two lines are optional. But their presence can help a lot as they
warn about problems in syntax and compilation. Generally, one should include
these lines in their program.

-> The next line is interesting. First, we have the special keyword 'foreach'
which takes a list as an argument and iterates through each of its elements
which takes the value of a scalar variable that is following 'foreach'. The
syntax is 'foreach $var (list) { .. }'. We won't talk about variables here,
but a variable that is assigned to a single item is called scalar and has the
'$' sign in front of its name. Now, you may ask where is this variable?
The Perl answer: it doesn't have to be assigned. It's not necessary. Every
element through iteration is stored to the special variable $_. Every loop
and other constructions in Perl that can assign items to single variables,
they can take advantage of that fact and use $_ instead of user's assigned
variables.

OK, what about the list? We have inside parenthesis the strange format
'reverse <>'. The reverse is a function that reverses a given list. Now, a
word about functions in Perl. In many cases their arguments don't necessarily
need to be enclosed in parenthesis. We could also write 'reverse(list)'. The list
in our cases is this strange operator, which is called the 'diamond' operator.
What this does is take as an input files that are passed as arguments in the
program in the command line and read each line of each file. It then
stores the reading lines in another special variable @ARGV. The symbol '@'
indicates that this is an array. (As we mentioned, we won't talk about
variables but think this as a list). If no files are passed as arguments,
then the program waits for input by the user. Each line the user enters,
is stored to the special variable @ARGV. The user can end his input by
pressing Ctrl + D (end of file in UNIX).

So what this line does? Without arguments passed on, the program waits for
user input. The user enters some lines, press Ctrl + D, the different lines
are stored in the array @ARGV and form a list. Next, the function 'reverse'
reverses the elements of the list and returns the reversed list. And with
'foreach' we iterate through each element of the reversed @ARGV and store it
to variable $_. I hope I didn't confuse you :) It was a little mess for me
at the beginning.

-> Lastly, what does the program do with each element? It just prints it. The
program uses the function print which takes a list of items as an argument and
prints them out. Remember that parenthesis is optional (not always) with
functions. So where is our argument? Because our program doesn't have an
argument for the print function, Perl pass to print the default variable $_
and because it is taken for granted that we mean this variable, we just don't
even need to state it. Good, huh? And so, Perl prints each line we gave in the
reverse order.

Wow, that was long. I hope that you find this explanation informative and motivate
you to learn Perl or any other programming language. I'm a starter too, but I
wanted to write this because I wanted to emphasize how beautiful programming is,
even with such small programs. If you want to learn more about Perl you can
visit this website:

https://www.perl.org/books/library.html

It has great free material. I'm currently studying 'Beginning Perl'. OK, that's it
for today. Everybody be well and see you in my next post!

PS. About the situation with Patrick, I was astonished by the immediate response of the
community. I hope that the issues will be resolved, with whatever decision he'll make,
and everybody including me will support him. I want to thank him and the rest of the team
for working professionally to provide us Slackware.
Posted in Uncategorized
Views 5405 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 01:36 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration