Converting to Perl - the sort of programs you will write
Archive - Originally posted on "The Horse's Mouth" - 2009-03-08 08:12:04 - Graham EllisI've put many of the examples that I wrote during last week's Perl Course into our converting to Perl resource, and they've available for use and download from there, subject to the usual "use at your own risk" exclusion!

There's a wide variety of examples in that directory, then ... let me give you some tasters and you can then explore further ...
• [link] to documentation and documentation comment example. With such a freeflow language as Perl, it's vital for you to comment it well and also provide user documentation.
• [link] to an example that uses list processing; in Perl, there are many occasions where you don't need to write code to process every elememt of a list - built in functions do the looping for you resulting in shorter and faster running code
• [link] to a context example. Languages like English use 'context' to add sense to works. If I write "Reading is a ...", you won't know if I'm referring to the town or the skill until I add the next work ... "Thames side" will tell you it's a town and "useful" will imply that it's probably the skill. Perl supports context too - you'll see different behaviours is some thing's used as a scalar, as a list, as a string within double quotes, or as an integer.
• [link] to a 'live' web log file analysis example. This example uses many of the fundamental Perl elements (list, hashes, regular expressions and more) for the rapidly written analysis of a file. As an example during the course, I analysed my 35 Mbyte log file from the previous day, and this example is the code I wrote. You'll see how many hits we had to each page, and a sorted table showing what proportion of our traffic was for each of the pages, sorted by page traffic levels.
Illustration - In Perl, you use the < and > pair to say "read from" a file handle; there are 4 file handles always open and available - for reading you have ~STDIN and DATA (STDOUT and STDERR are for output) and you simply read from them an assign into a variable. No need to open, no need to declare the variable they're going on to as it will be automatically created ....