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"
(written 2010-07-19, updated 2010-07-30)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P303 - Perl - Miscellany [2219] Configuring httpd, or Tomcat, to run CGI scripts in Perl - (2009-06-05)
[2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[3012] Exception handling in Perl - using eval - (2010-10-23)
[3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
P301 - Variables in Perl [975] Answering ALL the delegate's Perl questions - (2006-12-09)
[1581] What is an lvalue? (Perl, C) - (2008-03-18)
[1946] Variable Types in Perl - (2008-12-15)
[2241] Perl references - $$var and \$var notations - (2009-06-15)
[2374] Lead characters on Perl variable names - (2009-08-24)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)
[4608] Introspecion in Perl 6 - (2016-01-02)
P217 - Perl - More than Simple Lists and Hashes! [43] Hash of lists in Perl - (2004-09-09)
[293] Course follow-ups - (2005-04-27)
[1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
[2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3105] Adventure with references to lists and lists of references - (2010-12-26)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
[3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
[3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
P213 - Perl - Creating your own Classes [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[246] When to bless a Perl variable - (2005-03-15)
[983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
[1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
[1435] Object Oriented Programming in Perl - Course - (2007-11-18)
[1664] Example of OO in Perl - (2008-06-03)
[1864] Object Oriented Perl - First Steps - (2008-11-01)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2169] When should I use OO techniques? - (2009-05-11)
[2834] Teaching examples in Perl - third and final part - (2010-06-27)
[2969] What does blessing a variable in Perl mean? - (2010-09-24)
[3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
[3833] Learning to use existing classes in Perl - (2012-08-10)
[4607] Classes and object - first steps in Perl 6 - (2016-01-02)
P212 - Perl - More on Character Strings [453] Commenting Perl regular expressions - (2005-09-30)
[583] Remember to process blank lines - (2006-01-31)
[586] Perl Regular Expressions - finding the position and length of the match - (2006-02-02)
[597] Storing a regular expression in a perl variable - (2006-02-09)
[608] Don't expose your regular expressions - (2006-02-15)
[737] Coloured text in a terminal from Perl - (2006-05-29)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[943] Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
[1222] Perl, the substitute operator s - (2007-06-08)
[1230] Commenting a Perl Regular Expression - (2007-06-12)
[1251] Substitute operator / modifiers in Perl - (2007-06-28)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1336] Ignore case in Regular Expression - (2007-09-08)
[1510] Handling Binary data (.gif file example) in Perl - (2008-01-17)
[1727] Equality and looks like tests - Perl - (2008-07-29)
[1735] Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
[1947] Perl substitute - the e modifier - (2008-12-16)
[2230] Running a piece of code is like drinking a pint of beer - (2009-06-11)
[2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
[2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
[2801] Binary data handling with unpack in Perl - (2010-06-10)
[2874] Unpacking a Perl string into a list - (2010-07-16)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[3100] Looking ahead and behind in Regular Expressions - double matching - (2010-12-23)
[3322] How much has Perl (and other languages) changed? - (2011-06-10)
[3332] DNA to Amino Acid - a sample Perl script - (2011-06-24)
[3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
[3546] The difference between dot (a.k.a. full stop, period) and comma in Perl - (2011-12-09)
[3630] Serialsing and unserialising data for storage and transfer in Perl - (2012-02-28)
[3650] Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12)
[3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
[3927] First match or all matches? Perl Regular Expressions - (2012-11-19)
[4452] Binary data handling - Python and Perl - (2015-03-09)
Some other Articles
Tailoring of courses to meet customers needs - how it worksGetting in touch - Please allow me to see you when you are onlineNight Porter and reception - 24 hours a day?Program for reliability and efficiency - do not duplicate, but rather share and re-useFurther more advanced Perl examplesA long day in Melksham ...Another toot of the trumpetMoved - Melksham Lorry Park