Some more advanced Perl examples from a recent course
Archive - Originally posted on "The Horse's Mouth" - 2010-09-27 07:10:19 - Graham EllisI ran an extra Perl for larger projects course, single company, at the tail end of last week and into the weekend (the only gap in my diary for a few weeks!) and - as is often the case on single-company courses - I wrote some new illustrative code to show specific subjects that came up in a different way to they would on a more regular, choreographed course. Actuall I wrote so many that I'm only going to provide links to a handful - but I have added many more to our resources - you'll come across them from time to time if you're looking through our course modules and they'll all be listed in our very long global example, article and blog index.
To mention ...
• Perl doesn't start at the top of the code. You may learn as you delve deeped that it starts at a BEGIN block, and indeed it sorta-does. As you delve still deeper, you'll find that used code runs first, with it's begin block ... and you'll also find things like import, CHECK, INIT and even multiple BEGINs. Then at the end you'll find your END blocks and a whole series of DESTROYs if you have created objects ...
See [here] for an example with BEGIN, INIT and CHECK in use in the same code. And see [here] for an application that uses many of these extra, calling a module [here] with a class code. This latter example takes a horse and rider example; there's much more to having a horse than just buying it and taking it to shows ... a story this example tells!
• The switches to tr and y are rarely mentioned, but very useful. If you're not familiar with tr and y, they do a character by character translation ... which can be modified via a switch (a.k.a. modifier):
c - compliment - i.e. only change characters that do NOT match
s - squeeze - reduce multiple successive output characters to a single character (typically remove padding spaces)
d - delete the characters matched rather than replacing them
There's a new example [here], and a fresh example showing modifiers in matching [here].
• Variable scope is a huge subject - but not one that's naturally covered in peoples first delve into Perl; after all, if you're writing short bits of demo code, it's fine for the variables to be shared all around - the default - while you learn. But once you start needing to structure / segment your code so that it's not all in the same big tank, sloshing around, scope becomes important. By default, variables are global and in a package (i.e. namespace) called main, but you can assign them individually to different namespaces, or set a default namespace via a package. There's a "fundamentals" demonstration of the mechanism [here].
As global variables (the default) can be dangerous, use strict; may be specified to cause Perl to fail with an error if you use variable names that aren't explicity placed in a package, or declared with my, or local or our - the latter making them global, but saying to Perl "I know what I am doing here!". There's an example showing these different scopes, and how they vary, [here].
• Extending variable scope and polymorphism lead us on to Perl's OO capabiities - there are a whole load of examples from the notes and previous courses available in the creating your own classes and more objects module resources.
One new example from last week on the OO side is [this one], which shows a very wide range of features - base classes, inheritance, polymorphism, etc ... and also shows the use of SUPER to call a method from a base class (of course, as Perl allows multiple inheritance you'll be asking me "which base class" ... and the answer has to be that it calls the next method of that name on the search tree, ignoring the one in the current class).
• Over the years we've taught Perl, extras have been added to the language and although the huge change comes at Perl six, some things like given and ~~ are now with us in recent verstions of Perl 5. There was a when - given example just a couple of posts ago, and I have added a sample showing the intelligent match operator [here]. I suspect that in time most matches will be done with ~~ ... but not yet, as so much code needs to run with legacy versions of Perl which don't support it.
That's been a miscellany of examples from Friday and Saturday ... if you're interested in attending our public Perl courses, have a look [here] for details. If you've a group of delagates all wanting the same course at the same time, please ask - we can run private courses at our place, or at yours - and to an agenda that meets your needs.