Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Further more advanced Perl examples

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
P212 - Perl - More on Character Strings
  [4452] Binary data handling - Python and Perl - (2015-03-09)
  [3927] First match or all matches? Perl Regular Expressions - (2012-11-19)
  [3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
  [3650] Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12)
  [3630] Serialsing and unserialising data for storage and transfer in Perl - (2012-02-28)
  [3546] The difference between dot (a.k.a. full stop, period) and comma in Perl - (2011-12-09)
  [3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
  [3332] DNA to Amino Acid - a sample Perl script - (2011-06-24)
  [3322] How much has Perl (and other languages) changed? - (2011-06-10)
  [3100] Looking ahead and behind in Regular Expressions - double matching - (2010-12-23)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
  [2874] Unpacking a Perl string into a list - (2010-07-16)
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [2801] Binary data handling with unpack in Perl - (2010-06-10)
  [2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
  [2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
  [2230] Running a piece of code is like drinking a pint of beer - (2009-06-11)
  [1947] Perl substitute - the e modifier - (2008-12-16)
  [1735] Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
  [1727] Equality and looks like tests - Perl - (2008-07-29)
  [1510] Handling Binary data (.gif file example) in Perl - (2008-01-17)
  [1336] Ignore case in Regular Expression - (2007-09-08)
  [1305] Regular expressions made easy - building from components - (2007-08-16)
  [1251] Substitute operator / modifiers in Perl - (2007-06-28)
  [1230] Commenting a Perl Regular Expression - (2007-06-12)
  [1222] Perl, the substitute operator s - (2007-06-08)
  [943] Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [737] Coloured text in a terminal from Perl - (2006-05-29)
  [608] Don't expose your regular expressions - (2006-02-15)
  [597] Storing a regular expression in a perl variable - (2006-02-09)
  [586] Perl Regular Expressions - finding the position and length of the match - (2006-02-02)
  [583] Remember to process blank lines - (2006-01-31)
  [453] Commenting Perl regular expressions - (2005-09-30)

P213 - Perl - Creating your own Classes
  [4607] Classes and object - first steps in Perl 6 - (2016-01-02)
  [3833] Learning to use existing classes in Perl - (2012-08-10)
  [3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
  [2969] What does blessing a variable in Perl mean? - (2010-09-24)
  [2169] When should I use OO techniques? - (2009-05-11)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1864] Object Oriented Perl - First Steps - (2008-11-01)
  [1664] Example of OO in Perl - (2008-06-03)
  [1435] Object Oriented Programming in Perl - Course - (2007-11-18)
  [1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
  [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)
  [246] When to bless a Perl variable - (2005-03-15)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)

P217 - Perl - More than Simple Lists and Hashes!
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
  [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
  [3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
  [3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
  [3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [3105] Adventure with references to lists and lists of references - (2010-12-26)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
  [293] Course follow-ups - (2005-04-27)
  [43] Hash of lists in Perl - (2004-09-09)

P301 - Variables in Perl
  [4608] Introspecion in Perl 6 - (2016-01-02)
  [4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [2374] Lead characters on Perl variable names - (2009-08-24)
  [1946] Variable Types in Perl - (2008-12-15)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)

P303 - Perl - Miscellany
  [3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
  [3012] Exception handling in Perl - using eval - (2010-10-23)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
  [2219] Configuring httpd, or Tomcat, to run CGI scripts in Perl - (2009-06-05)


Back to
Different perl examples - some corners I rarely explore
Previous and next
or
Horse's mouth home
Forward to
Program for reliability and efficiency - do not duplicate, but rather share and re-use
Some other Articles
Tailoring of courses to meet customers needs - how it works
Getting in touch - Please allow me to see you when you are online
Night Porter and reception - 24 hours a day?
Program for reliability and efficiency - do not duplicate, but rather share and re-use
Further more advanced Perl examples
A long day in Melksham ...
Another toot of the trumpet
Moved - Melksham Lorry Park
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2877_Fur ... mples.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat