A NOTE ABOUT THIS POST: Programming Perl Version 3.0 - This is a very good book for reference. This is my own version of extracts from the book. I would have included most of the concepts that I have learned and understood.
1. An Intro into the PERL programming language
Is PERL an easy language to learn?
If you ask this question the answer is both yes and no. As anyone else I too had compared PERL with other programming or scripting languages like python and ruby. Like in the end what you will find out each one has its own advantages.
Let me state up front the best use of PERL
1.System Administration
2.Data Extraction ( RegEx comes very handy here)
3.File Manipulation tasks ( With perl you can do what you find difficult to achieve using the shell scripts)
4.The CPAN provides the most comprehensive library to support any tasks. Hence installing the modules from the CPAN and calling the appropriate library routines makes the world an easier place to live.
Ok, anything in the world that has advantages has the disadvantages.
1.Lot of ugly programs can be written using PERL like robots, scrapers.
2.The perl code is difficult to understand and read since we may not be following some predefined standards in PERL. The issue of following the conventional programming is addressed in PERL 3.0
2.Variables
Different types of variables available in perl
Type Character Example Is a name for:
Scalar $ $value String and Integer values can be stored here
Array @ @books An array of values
Sub routine & &leash A callable chunk of perl code
Typeglob * *james Everything named james
2.1 Singularities
Speaking of variables we must know the distinction between the often used words singularities and pluralities in the perl context. The scalar variable is used in singular context meaning it is used to hold a value in a variable name and the $var_name is used to access, assign and modify the variable name.
The scalar variable can be used to hold - integers, floating-point numbers, strings, and even references to other variables, or to objects
$ans = 2; # an integer
$pi = 3.14; # a "real" number
$avocados = 6.02e23; # scientific notation
$pet = "Positon"; # string
$sign = "I love my $pet"; # string with interpolation
$cost = 'It costs $100'; # string without interpolation
$thence = $whence; # another variable's value
$exit = system("vi $file"); # numeric status of a command
$cwd = `pwd`; # string output from a command
Scalar references to objects
$post = new Position "King";
if (not $post) { die "Object not created! "; }
$post->assign();
Here we create a reference to a Position object and put it into the variable $post. Next, we test $post as a scalar Boolean to see if it is "true", and we throw an exception if it is not true, which in this case would mean that the new Position constructor failed to make a proper Positon object. But on the last line, we treat $post as a reference by asking it to look up the assign() method for the object held in $post, which happens to be a Positon, so Perl looks up the assign() method for Positon objects. The context is important in Perl because that's how Perl knows what you want without your having to say it explicitly, as many other computer languages force you to do
2.2 Pluralities
Some kinds of variables hold multiple values that are logically tied together. Perl has two types of multivalued variables: arrays and hashes. In many ways, these behave like scalars--they spring into existence with nothing in them when needed, for instance. But they are different from scalars in that, when you assign to them, they supply a list context to the right side of the assignment rather than a scalar context.
2.2.1Arrays
An array is an ordered list of scalars, accessed by the scalar's position in the list. (It might also contain references to subarrays or subhashes.) To assign a list value to an array, you simply group the values together (with a set of parentheses):
@home = ("couch", "chair", "table", "stove");
As in C, arrays are zero-based, so while you would talk about the first through fourth elements of the array, you would get to them with subscripts 0 through 3
$home[0] = "couch";
$home[1] = "chair";
$home[2] = "table";
$home[3] = "stove";
Conversely, if you use @home in a list context, such as on the right side of a list assignment, you get back out the same list you put in. So you could set four scalar variables from the array like this:
($potato, $lift, $tennis, $pipe) = @home;
These are called list assignments. They logically happen in parallel, so you can swap two variables by saying:
($alpha,$omega) = ($omega,$alpha);
2.2.2 Hashes
A hash is an unordered set of scalars, accessed by some string value that is associated with each scalar. For this reason hashes are often called associative arrays. But that's too long for lazy typists to type, and we talk about them so often that we decided to name them something short and snappy. The other reason we picked the name "hash" is to emphasize the fact that they're disordered. (They are, coincidentally, implemented internally using a hash-table lookup, which is why hashes are so fast, and stay so fast no matter how many values you put into them.) You can't push or pop a hash though, because it doesn't make sense. A hash has no beginning or end. Nevertheless, hashes are extremely powerful and useful. “Until you start thinking in terms of hashes, you aren't really thinking in Perl”.
This is correct.
%longday = ("Sun", "Sunday", "Mon", "Monday", "Tue", "Tuesday",
"Wed", "Wednesday", "Thu", "Thursday", "Fri",
"Friday", "Sat", "Saturday");
But this is easier to interpret.
%longday = (
"Sun" => "Sunday",
"Mon" => "Monday",
"Tue" => "Tuesday",
"Wed" => "Wednesday",
"Thu" => "Thursday",
"Fri" => "Friday",
"Sat" => "Saturday",
);
