Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Different perl examples - some corners I rarely explore

The private Perl course that I ran on Wednesday through Friday of last week was a little out of the ordinary as we were concentrating far more that usual on a wide variety of practices that may be found - either in legacy code or advanced recent code. Great fun for me, and plenty of new examples.

Here are some notes (part 1 or 2) which I wrote up for the delegates - if something in the summary notes has attracted your attention to this page, follow the program name link just above it, and you'll find the full source code I am talking about.

about albert in module P224
about victoria in module P224

1. Set up a SIMPLE signal handler under signals. Nothing complex as this happened asynchronously and there's a danger of actions at the wrong moment - far better to set a flag and process at a specified time in the main program loop

2. Two ^C presses in 3 seconds cause the code to exit; a single ^C just gives a message

3. You can fork a process to give a parent and child, each of which goes off to do its own thing. Fork returns "0" to the child, and the pid of the new process to the child - that's how you tell them apart and have them each go their on way

4. Processes can talk to each other through a pipe; open the pipe before you fork, and when you fork you end up with two pipes - the write handle on each process sending to the read handle on the other. Processes can be told to read from each other via a signal (not illustrated in this example)

about assume in module P203

Perl assues that variables start with an empty string, which when used in a numeric context gets converted into a zero.

It is doubtful if it is good practise to make use of this default initialization, and if you run perl with the -w option you'll get a warning message to let you know about the potential problem

about biggest in module P602

Opendir, readdir and closedir are the recommended way of going through a lot of files on the file system - they don't start a fresh shell and they don't waste time sorting the entries into order. So they run quickly and are not operating system dependent.

You'll get back all file names and directory names - including "." (the current directory) and .. (the parent) and if you're descending into child directories, you must eliminate these!

about bip in module P206
about tae in module P206

There are multiple ways of doing most things in Perl - this example shows five different ways of writing an "if" (and it doesn't even start with the unless conditionals!), and four different forms of comparator - eq == =~ and ~~.

The do ... while loop is a great way to check user input and to reprompt as often as necessary to get a valid response. Ideally, wrap it in a sub ...

about cafe3 in module P218
about dish.pm in module P218
about cafe in module P218

The Exporter module supplied with Perl lets you offer variables that are within your package in the parent namespace too. i.e. it lets you refer to them by simple names such as $n_dishes rather than (in my example) $dish::ndishes.

When your use has no parameters, you'll import automatically everything in the @EXPORT list. Empty parameter brackets specify there are to be no imports at all, and brackets with specified names call up those names - which must have been listed within the class in @EXPORT or @EXPORT_OK lists.

about chopin in module P202

Don't get caught by
  print (chop($line));
which will alter $line and just print a new line, or
  print (chomp($line));
which will alter $new line and print out the value "1" or "0".

If you want to print out $line with the new line removed, do the chop or chomp in a separate line before the print!

about ctx in module P208

If you refer to a list in your Perl program, it will work with the whole list. But if you refer to it where only a scalar makes any senses, it will assume you mean the length of the list ... and if you refer to a list within double quotes, all the elements will be joined with a space between them.

There's an example of the various formats in this example, and it also shows how you can refer to individual list elements, the index number of the last element (one less than the number of elements in the list!) and list slices. Finally, it shows you how you can split a line of text into pieces and then name each of the pieces in a scalar al in one line!

about dadu in module P219

A demonstration of the Data::Dumper module supplied with Perl which lets you prin out the contents of a data structure (actually, the Dumper method returns a string so that you can be much more flexible!)

I've also added a $debug variable in my demo which you can comment out to remove tracing from your program - a useful trick in code that needs testing and analysis from time to time, under the programmer's control.

about delay in module P210
about johnny.vegas in module P210
about kbcheck in module P305

Perl's built in select function, without arguments, tells you where the default output from print is currently directed. With a file handle as an argument, it sets that default output to the specified handle.

With multiple parameters, select lets you check whether input is waiting on any input channel, and whether an output channel is open and available. This uses bit masked parameters and vec - so it's an "interesting" piece of code - but very useful if you need to do background work while waiting for the user to answer a question. See also fork and signal ;-)

about dend in module P215

If you add data to a Perl program after the __END__ marker, you can read it from the DATA file handle using at run time. DATA is open and available to you when you run your program, as are STDIN, STDOUT and STDERR. Other file handles must be opened.

about dotty in module P215

If you read from a file handle that contains * or ? characters, Perl assumes that you mean to return file names that match the pattern given. So
  while () {
will report on all files with a "." in their name (remember this is not regular expressions - it's file name globbing!)

about evx in module P303

If you run code in an "eval", and it hits a condition which would normally cause perl program to halt with an error, you'll get back the error message in $@ and the calling code will continue to run. In effect, this is exception handling - try and catch - in Perl.

If you pass a block to eval using { ... }, the block wil be compiled with the main program. But if you pass the block in using " ... ", the block will be compiled just prior to each execution - more flexible, but slower!

about file in module P215

Operations such as -e to check if a file exists usually run on a named file, passed in as a string. But there are two special cases:

1. If you do not specify any parameter, the file who's name is the current contents of the $_ variable will be used

2. If you specify _ (no $ in front of it), then Perl will simply return you more information about the last file you asked about. This is useful and efficient - internally, the operators like -e and -s return a full stat report which is buffered in case you want to ask more about the same file!

about funcall in module P209

=> can be used to replace , in the definition of a hash - it's useful as it helps you tell the keys and values apart, and it allows you to drop the quotes of the key if the key is a bare word with no other meaning in that context and scope.

But you can go further - you can use the => in a sub call in place of a comma, and it gives the effect of named parameters. It's common practise for module authors to collect parameters in this way, and also to accept the bare words with a leading minus sign, so that the issue of them having another meaning in this scope goes away.

This example shows the minus sign and =>in use in the calling of a sub, and also an example of how the sub would be written.

about gawd in module P301

A Perl variable name starting with "*" is a typeglob - that's one each of a sclar, list, hash and file handle. They're used / useful in handline (in effect) lists of file handles through soft references.

Note that a typeglob is in fact a reference - so if you assign a typeglob and then change the data via the original name, you're also changing the data pointed to by the new name.

about goflavours in module P303
about gogo in module P303
about pmg in module P303

Perl has a goto statement (3 flavours actually), and you can also use the last command to jump out of a block, which some people also consider to be a form of the goto statement.

As well as giving a label to the goto statement, you may give a variable of expressing, and Perl will jump to the label named in that string.

Finally, if you pass a named piece of code (a sub) to Perl's goto, it will jump to that sub and fix its stack so that it look like the original was never called.

Only the final type - the "magic goto" - should really be used these days, and that for the specific case of the dynamic loading of code. Bring in code through a require, then goto it!

(written 2010-07-18, updated 2010-12-04)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P602 - Perl - Advanced File and Directory Handling
  [839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)
  [1225] Perl - functions for directory handling - (2007-06-09)
  [1709] There is more that one way - Perl - (2008-07-14)
  [1832] Processing all files in a directory - Perl - (2008-10-11)
  [1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
  [3412] Handling binary data in Perl is easy! - (2011-08-30)
  [3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09)

P305 - Perl - User Input
  [2213] Keyboard reading in Perl - character by character not line by line - (2009-06-01)
  [2382] Giving up on user input - keyboard timeout in Perl - (2009-08-28)

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)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [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)

P302 - Perl - The Selfloader
P224 - Perl - Intersystem Communications
  [604] Perl - multiprocess applications - (2006-02-13)
  [1073] Heartbeat script in Perl - (2007-02-09)
  [1918] Perl Socket Programming Examples - (2008-12-02)
  [2402] Automated Browsing in Perl - (2009-09-11)
  [2695] TCP v UDP / Client v Server - Python examples - (2010-03-25)
  [2970] Perl - doing several things at the same time - (2010-09-25)

P218 - Perl - More Objects
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [246] When to bless a Perl variable - (2005-03-15)
  [531] Packages in packages in Perl - (2005-12-16)
  [588] Changing @INC - where Perl loads its modules - (2006-02-02)
  [592] NOT Gone phishing - (2006-02-05)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [930] -> , >= and => in Perl - (2006-11-18)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [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)
  [1665] Factory method example - Perl - (2008-06-04)
  [1819] Calling base class constructors - (2008-10-03)
  [1949] Nuclear Physics comes to our web site - (2008-12-17)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2811] Igloos melt in the summer, but houses do not - (2010-06-15)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [3097] Making Perl class definitions more conventional and shorter - (2010-12-20)
  [3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
  [3377] What do I mean when I add things in Perl? - (2011-08-02)
  [3581] Perl - calls to methods that use => - what do they mean? - (2012-01-16)
  [3941] Building an object based on another object in Perl - (2012-12-03)
  [4096] Perl design patterns example - (2013-05-20)
  [4098] Using object orientation for non-physical objects - (2013-05-22)
  [4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)

P210 - Perl - Topicalization and Special Variables
  [493] Running a Perl script within a PHP page - (2005-11-12)
  [639] Progress bars and other dynamic reports - (2006-03-09)
  [969] Perl - $_ and @_ - (2006-12-07)
  [1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
  [1221] Bathtubs and pecking birds - (2007-06-07)
  [1232] Bathtub example - (2007-06-14)
  [1289] Pure Perl - (2007-08-03)
  [1444] Using English can slow you right down! - (2007-11-25)
  [1508] How not to write Perl? - (2008-01-15)
  [1704] Finding operating system settings in Perl - (2008-07-10)
  [1705] Environment variables in Perl / use Env - (2008-07-11)
  [1728] A short Perl example - (2008-07-30)
  [1829] Dont bother to write a Perl program - (2008-10-10)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1922] Flurinci knows Raby Lae PHP and Jeve - (2008-12-04)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [3449] Apache Internal Dummy Connection - what is it and what should I do with it? - (2011-09-19)
  [4301] Perl - still a very effective language indeed for extracting and reporting - (2014-09-20)
  [4395] Preparing data through a little bit of Perl - (2015-01-15)
  [4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
  [4700] Obfurscated code - it might work, but is it maintainable? - (2016-07-02)

P203 - More about the Perl Environment
  [328] Making programs easy for any user to start - (2005-05-29)
  [743] How to debug a Perl program - (2006-06-04)
  [748] Getting rid of variables after you have finished with them - (2006-06-06)
  [1865] Debugging and Data::Dumper in Perl - (2008-11-02)

P202 - Perl Fundamentals
  [184] MTBF of coffee machines - (2005-01-20)
  [1312] Some one line Perl tips and techniques - (2007-08-21)
  [1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
  [1726] Hot Courses - Perl - (2008-07-28)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1946] Variable Types in Perl - (2008-12-15)
  [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [3329] Perl from basics - (2011-06-20)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [4324] Learning to program - variables and constants - (2014-11-22)

Back to
A long day in Melksham ...
Previous and next
Horse's mouth home
Forward to
Further more advanced Perl examples
Some other Articles
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
Different perl examples - some corners I rarely explore
A long day in Melksham ...
Unpacking a Perl string into a list
Another toot of the trumpet
Moved - Melksham Lorry Park
Moving from Python 2.6 to Python 3
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., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2876_Dif ... plore.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb