Seven new intermediate Perl examples
Archive - Originally posted on "The Horse's Mouth" - 2008-10-30 07:46:38 - Graham EllisFrom the "learning to program in Perl" course I'm running this week, I am pleased to present seven new examples ... written during the course, in front of the delegates, to show them NOT ONLY how the code works, BUT ALSO how a programmer will develop such code.
Lists and Context
* If you use an @abc, you mean the WHOLE of a list
* If you use $abc[10], you mean a scalar member of a list
* If you use $#abc, you mean the top index number of a list
* If you refer to a list IN A LIST CONTEXT you mean all elements
* In a SCALAR context, you mean the NUMBER OF ELEMENTS in the list
* In a DOUBLE QUOTE context, you mean a string of all element ...
... linked with a space between each of them
See source code at A demonstration of lists and context
Handling command line options with getopts
The getopts sub (in the Getopt::std module that's supplied with the Perl distribution) lets you process command line parameters (from @ARGV) ... filtering out options into flag variables without the need for you to write (and maintain) that code yourself.
See source code at getopts - command line handling in Perl
UK Postcode matching in Perl
This is a testbed program to validate UK postcodes and also to extract the various key elements into other variables.
The section of a postcode up to the mandatory space is known as the "in" as it's inbound to a sorting office, and the section after the space is known as the "out" as it's used to sort at the sorting office into individual post staff delivery runs.
I have used the \b boundary condition in this code - I did NOT want to anchor to start or end of line, but neither did I want to match postcodes that start within a word. \b (word boundary match) is a happy compromise.
See source code at Regular Expression - match and extract from a UK postcode
Running another process on a pipe
If you use backquotes, a command is run on your computer as a separate process and WHEN IT IS COMPLETED, you'll get the results back. If you want to read from the other process as it runs, you can open a piped process instead - that's using open with a pipe character at the end of what would normally be the file name, but is now the command to run your extra process in turn.
See source code at piping a ping into your perl
Splitting Money many ways
Scenario - You have 21 nieces and nephews, and you're going to spend a certain amount of money on them for presents - let's say a total of 1000 pounds. But some of the nieces and nephews may not be in favour (for example, you were not thanked for last year's gift) so you'll split their share amongst the others.
Produce a table, nicely formatted, showing how much money you'll be spending on each child depending on how many are in favour.
See source code at Formatting money and other text in Perl
Single, Double and backquoted strings
In Perl, you can write ...
* A literal string in single quotes
* A string in which \ $ and @ are expanded in double quotes
* An operaring systems command to be run in backquotes
and in each case, the result will be returned to you as the result of the operation if you want to save it into a variable.
If you really want a double quote character within a double quoted string, you can always add an extra \ in front of it - but that facility is NOT similarly available to you if you want a single quote in a single quoted string. So you have to use the alternatives ...
* qq followed by any non-alphanumeric delimter of your choice gives you a double quoted string
* q followed by any non-alphanumeric delimter of your choice gives you a single quoted string
* qx followed by any non-alphanumeric delimter of your choice gives you a shell executed command
Finally, you can write multiline strings in what are know as "here" documents, where the delimiter is a complete string and must occur further down on a line ON ITS OWN (no ;, no spaces) to indicate the end of the block.
See source code at single double and backquotes in Perl
Tiny code in Perl
The code in this example uses the -n option which implies that it runs within a loop that reads each line in turn from a file named on the command line and stores it in the "default input and pattern matching space" a.k.a. $_ ...
It's open to debate whether or not coding in this way is good practise - my personal view is that it's great for occasional utilities where you just need to quickly filter a data file on one or two occasions, but it's not a good idea for long term coding where you need to have maintenance in mind.
See source code (but it's very short indeed!) at using an implicit loop with -n
In all these new examples, I have added an __END__ to the end of my source code to stop the Perl interpretter at that point, and then added in some sample output so that you can see what the code produces when it runs. There are plenty of other resources / samples accessible from the sample source pages too.
We have a public Using Perl on the Web and a public Perl for Larger Projects course both coming up just before Christmas [2008], with space available. Both courses will run again in 2009 ... please follow the links in this paragraph for the next dates if you've missed Christmas!