Perl - Getting started

From LXF Wiki

(Difference between revisions)
Revision as of 15:56, 22 Jun 2010
Mine0311 (Talk | contribs)
Getting started
← Go to previous diff
Revision as of 19:52, 26 Jun 2010
Guy (Talk | contribs)
rv spam
Go to next diff →
Line 16: Line 16:
How can you become a Perl Wizard? Well, first of all, study “Programming Perl” and the many other books covering every possible side of this language (the best ones are published by O'Reilly, http://perl.ora.com). For online documentation, visit the Comprehensive Perl Archive Network (www.cpan.org) instead: the complete collection of Perl Modules, together with plenty of tutorials and more technical guides, can be found there. Specific question should be posted instead to the comp.lang.perl.misc newsgroup. How can you become a Perl Wizard? Well, first of all, study “Programming Perl” and the many other books covering every possible side of this language (the best ones are published by O'Reilly, http://perl.ora.com). For online documentation, visit the Comprehensive Perl Archive Network (www.cpan.org) instead: the complete collection of Perl Modules, together with plenty of tutorials and more technical guides, can be found there. Specific question should be posted instead to the comp.lang.perl.misc newsgroup.
</div> </div>
- 
-= Getting started = 
- 
-A lot of basic Perl syntax rules like string quoting are equal, or very similar, to those of Unix shells. There is a lot of good and easy documentation on this, starting from my command line tutorial in the last numbers of Linux Format which you obviously read, didn't you? For this reason, during this series I will focus on the most peculiar parts of Perl, starting from how to read it without getting an headache. I love Perl dearly, and use it almost daily for the most varied tasks, but must warn you: Perl has been called the only language that looks the same before and after encryption, and there is some truth to this, if one doesn't know how the language is structured. Let's get this [http://www.fordpartsgateway.co.uk/fordmondeoparts.php ford mondeo parts] out of the way then. 
- 
-= Perl Variables = 
- 
-Probably the first thing that hurts the eyes of Perl newcomers is the fact that all variables have a non-alphabetical prefix. This, according to detractors, fills the screen with unreadable clutter. But does it? As far as I am concerned, I find useful to have these markers, because they immediately tell me what is the inner structure of the corresponding variable, and what I can do with it. This is what I'm talking about: 
- 
-<pre> 
-$NAME = 'Johnny'; 
-$AGE = 33; 
-@BOOK = ('Introduction', 'First Chapter', 'Second Chapter'); 
-%STAR_WARS_FAMILY = ('Father', 'Anakin', 
- 'Mother', 'Padme', 
- 'Male Twin', 'Luke', 
- 'Female Twin', 'Leia'); 
-</pre> 
- 
-The first two variables are scalars. Scalars in Perl are anything that is composed, so to speak, of one piece only. Their names always start with a dollar sign. String scalars are always delimited by quotes, like their shell equivalents, whereas numbers are not: 
- 
-<pre> 
-$A = 'Hello'; 
-$B = '$A Johnny!'; 
-$C = “$A Johnny!”; 
-$D = `ls /usr/bin`; 
-</pre> 
- 
-Here $B will contain just the exact characters we wrote inside it. $C instead, courtesy of the double quotes, will be equal to “Hello Johnny!”. Finally, thanks to the inverted quotes, $D will contain the whole result of executing the “ls /usr/bin/” command at a shell prompt. 
- 
-Ordered lists of scalars, indexed by number, are just called arrays and marked with an initial @. They are good for storing things naturally organized in a numeric order. A single piece of an array is indicated with the $ prefix, the array name and its index (starting from 0!) in square brackets. Consequently, the content of the second element of @BOOK above, that is $BOOK[1], will be 'First Chapter'. 
-Associative arrays, or hashes for short, are the last main category of Perl variables. A hash is a group of scalars which are indexed, or referred to, not with a number but through other scalars (keys), which express the role or meaning of each value inside the whole hash. In other words, Perl natively allows us to say that the $STAR_WARS_FAMILY{'Father'} is 'Anakin', without extra syntax or instructions. Each key or value of an hash can be a different kind of scalar than the others. 
- 
-So, yes, even the shortest Perl Script is filled with $, @ and # characters at every corner. But we can create very complex data structures in a snap and, with a bit of care when naming their elements, still remember easily what each piece is. This is made even more powerful, if not dangerous, by the fact that you can nest arrays and hashes at will, like in this example: 
- 
-<pre> 
-%STAR_WARS_FAMILY = ( 
- 'Father', {'Name', 'Anakin', 
- 'Height (cm)', 210, 
- 'jobs', ['Slave', 'Podracer pilot', 'Jedi Knight','Darth Fener']}, 
- 'Mother', {} # other similar data here 
- ); 
- 
-print "$STAR_WARS_FAMILY{'Father'}{'jobs'}[2]\n"; 
-</pre> 
- 
-Here is an hash in which each first level component is another hash containing the 'Name', 'Height' and 'jobs' elements. The 'jobs' one is actually an array. Note the use of curly and square brackets to distinguish between hashes and arrays, however nested. The net result of all this definition is that the print instruction at the end will produce (but you already knew it, right?) a “Jedi Knight” output. 
-Last but not least, there is a built-in scalar variable which is necessary to know. Sometimes Perl programmers seem to forget to write which variable they are thinking to: 
- 
-<pre> 
-print if some_condition_is_true 
-</pre> 
- 
-Here is a print instruction that says to print if something (never mind what) happens, but what should we print? Which variable? In Perl, when this happens, the interpreter uses the current value of the default variable, which can be omitted as I did, or indicated by the $_ symbol. “Default” must be intended as “the result of the previous operation, or the current variable of the loop we are in”. Just remember this and a lot of code will start to look a bit clearer. 
- 
-= Operators = 
- 
-There are really a lot of Perl operators. Some can only be applied to one class of variable, be it scalars, arrays or hashes. Other assume a different meaning depending from which kind of variable they are working on. Therefore here I will only be able to present a quite small subset of them. The basic numerical operators are like those in the C language: 
- 
-<pre> 
-$A =10 * 7.5 - 16;  
-$B = $A % 4; # division modulo 4 
-</pre> 
- 
-Both numbers and alphabetic variables can be auto-incremented: 
- 
-<pre> 
-$A = 4; 
-$B = 'ab'; 
-$A++; # now $A is equal to 5... 
-$B++; # and $B is equal to 'ac'!!  
-</pre> 
- 
-Isn't it cool how letters can be incremented too? You can also concatenate strings, one or more times; 
- 
-<pre> 
-$FIRST_NAME = 'Luke'; 
-$LAST_NAME = 'Skywalker'; 
-$COMPLETE_NAME = $FIRST_NAME.' '.$LAST_NAME; 
-$REPEATED_NAME = $FIRST_NAME x 3; 
-</pre> 
- 
-$COMPLETE_NAME is now equal to “Luke Skywalker” and $REPEATED_NAME to “LukeLukeLuke”. 
-You can compare scalars in many different ways. Let's list some of them first, and then figure out what all this choice means: 
- 
-<pre> 
-$A < $B; # is number $A greater than number $B? 
-$A lt $B; # does string $A comes first than string $B? 
-$A <=> $B; # which number comes first, $A or $B? 
-$A cmp $B; # which string comes first, $A or $B? 
-</pre> 
- 
-See what I meant? Some comparisons only make sense when both variables are numbers, or both strings. Here, the statement “This string comes first than this other” must be intended in alphabetical, not numerical order. Even if one or both string contained only numbers, using a string-related operator would force an alphanumerical comparison. To see all this in action, type this Perl command just as it is at the prompt, and note which of the two comparisons holds true: 
- 
-<pre> 
-perl -e 'if ("dude" lt "hello") {print "dude!\n";} if ("dude" < "hello") {print "Hello!\n";}' 
-</pre> 
- 
-As a matter of fact, only the first test succeeds doing what we expected: the first one, whose operator is consistent with the operands, since it means “does this string comes before the other in alphabetical order?”. In the second test, using the “<” operator instead of the “lt” one, we basically asked Perl to check if the **number** 'dude' is smaller than the **number** 'hello'. The result is predicatbly unpredictable. Also remember that Perl automatically converts back and forth between strings and numbers at every instruction, depending from the context. This can be a powerful programming aid or a source of great confusion, depending from how often one forgets it...  
-Enough about scalars, on to arrays, now. Perl can manipulate them directly in many ways. Single elements of an array, for example, can be easily pushed or popped from it: 
- 
-<pre> 
-@Jedi = ("Joda", "Obi Wan"); # some Jedi knights 
-push(@Jedi, "Anakin"); # Young Anakin enters the order 
-$gone_to_the_dark_side = pop(@Jedi); # Now Anakin isn't a Jedi any more 
-</pre> 
- 
-There also are operators for dealing with hashes. Instructions like these are extremely common in Perl scripts: 
- 
-<pre> 
-while (($Actor, $Character) = each(%STAR_WARS_CAST)); 
-# or also 
-@ACTORS_ONLY = keys(%STAR_WARS_CAST); 
-</pre> 
- 
-The first line would loop over the %STAR_WARS_CAST hash, assigning each time the current key and its associated value to a couple of scalars ($Actor and $Character), which can be processed as you like. The key operator is instead used when you only need to save the keys of an hash in a separate data structure, and work on them from now on. 
- 
-[[Category:Perl]] 

Revision as of 19:52, 26 Jun 2010

Perl tutorial part 1

(Original version written by Marco Fioretti for Linux Format magazine issue 69.)


We explain how to keep data under control with a powerful scripting language.


Scripting languages are very popular because they take care by themselves, if sacrificing some performance, of boring details like defining variables, allocating memory, releasing it to the operating system and so on. Perl (www.perl.com) still is, without doubts, one of the most popular languages in this category. This Practical Extraction and Report Language was invented in the late 80s to manipulate huge quantities of text, and it is still mighty good at it. Did you know, for example, that Perl is also credited for saving the Human Genome Project? If not, read the full story at http://bioperl.org/GetStarted/tpj_ls_bio.html. In the 90s Perl was widely used to generate dynamic web pages via CGI scripts. Nowadays PHP (www.php.net) is probably a more popular solution in that space, but Perl still has a lot of loyal fans and an immense base of scripts, extensions (called modules) and documentation that can be readily adapted to anyone's needs.

Above all, you don't have to be a system administrator or professional programmer to benefit from this language. Perl can help whenever you need to quickly process some text or (why not?) images. It doesn't even matter if the files you want to work on are on your local drive or somewhere on the Internet. Perl has lots of modules to deal transparently with files on remote servers.

Where to learn Perl in depth

How can you become a Perl Wizard? Well, first of all, study “Programming Perl” and the many other books covering every possible side of this language (the best ones are published by O'Reilly, http://perl.ora.com). For online documentation, visit the Comprehensive Perl Archive Network (www.cpan.org) instead: the complete collection of Perl Modules, together with plenty of tutorials and more technical guides, can be found there. Specific question should be posted instead to the comp.lang.perl.misc newsgroup.