Main Content

Further more advanced Perl examples

Archive - Originally posted on "The Horse's Mouth" - 2010-07-19 05:58:45 - Graham Ellis

I've uploaded a further batch of new examples (that makes around 40 in total!) from the private Perl course that I ran from Wednesday through Saturday last week - many of them adding a new twist on to previous examples. If you read a comment below and thing "that's what I'm looking for an example of", then simply click on the code sample name just above, and you'll find a complete source program.

grepplus in module P208

Perl's grep function isn't just a regular expression processor - it puts each value into $_ and lets you perform other tests too. And $_ is set as a pointer, so if you alter it you're changing the incoming list. Although it may be regarded as clever, this changing of the incoming variable can be very confusing to the reader and is not recommended!

iiw in module P301

Variables default global in Perl 5.

If you declare a variable as my, it is then lexcially scoped to the block in which it occurs - i.e. only available under that name within the block, and lost at the end (unless a reference to it is used elsewhere).

If you declare a variable as local, any old global variable of the same name is saved away until the end of the current block and a new one created.

Since a local variable is not lexically scoped, it can be used within a sub under its original name, whereas a my variable cannot.

Strong recommendation is to use my rather than local, though there are times you'll want to use local in order to save and restore special variables such as $".

lili2 in module P217

You don't have two dimensional arrays in Perl - but you can have "lists of ists". And these are much more flexible!

At first glance, the syntax is complex ... but you'll soon learn that Perl, ever practical, lets you use a shorthand that's the same as you might use to address them as if they were 2 dimensional arrays anyway!

need1 in module P213
need2 in module P213
needbr in module P218
teacup.pm in module P218

You can use a bare word as a function call name provided that you've defined the function (i.e. sub) prior to the compiler finding the word. That means it has to be defined higher in the code, or in a module that's loaded with a use (a require will not suffice!)

You can force a bare word to be a sub call by adding () onto the end of the name, or by adding & before the name.

Perl's warnings (-w) will tell you of any bare words that occur in your code which are not run as subs - "Useless use of a constant in void context".

If you want conditional loading, then use require. It pulls in code at run time rather than compile time ... our example loads one of two different modules depending on whether the second number is odd or even!

protodemo in module P303

You don't usually declare a Perl sub with bracketed parameters - but you CAN use a function prototype if you wish. That lets you specify the number and type of parameters that it's called with, and you'll be rejected at compile time if you get the calling code wrong.

runner.pm in module P218
runtest in module P218

runner.pm is a pair of classes, both descended off a parent class, and with errors reported via the Carp module to make them useful to the calling program. It's a short but heavily featured demo showing polymorphism, inheritance, multiple packages (classes) in a file, and a common AUTOLOADer

slurpex in module P212

You can read an entire file into a string by undef-ing $/, or with the read function. And you can then do regular expression matches against the whole string - sometimes that's better than splitting it into a list. Remember not to use $' $` and $& if you want it to be efficient, and remember the m modifier on the regular expression so that ^ and $ will match at embedded new lines!

sting in module P212
stuff in module P212

Single and double quotes strings ... qw, q and qq ... here documents. You have plenty of choice in defining strings!

Using qr, you can compile a regular expression into a variable, and that's going to be very efficient if it contains variables that change just a few times as you program runs.

waw in module P303

Some interest functionality:
* caller - tells you about your callin code
* wantarray - tells you if you're in a list context
* ref - tells you whether a variable is a reference, and is so to what sort of collection (or maybe "SCALAR"