Exercises, examples and other material relating to training module P210. This topic is presented on public courses
Not all Perl variables are created as you run your code. Some useful information is provided in special variables before your code is even reached, and some of these special variables can be manipulated to affect the ongoing operation of your program. Perl even provides a facility that lets you leave parameters out of your source code, and it will assume a default current variable. This is known as topicalization.
Articles and tips on this subject | updated |
4700 | Obfurscated code - it might work, but is it maintainable? And here is my latest entry in the "how many special characters can I put together in a statement without a single letter digit or space between them" competition.
perl -n -e '/([^"]+@[^"]+)/ and print("$1\n");' ParishCouncillors-2.csv
Perl, of course!
I can teach how to write maintainable ... | 2016-07-04 (short) |
4682 | One line scripts - Awk, Perl and Ruby The art of the one line script, originating in awk and passing through Perl lives on in Ruby!
WomanWithCat:4c grahamellis$ awk '/Upper/{c++}; END { print(c,"of",NR)}; BEGIN {c=0}' rstats2015.txt
5 of 2537
WomanWithCat:4c grahamellis$
WomanWithCat:4c grahamellis$ ... | 2016-05-20 |
4395 | Preparing data through a little bit of Perl A Java course - and yet I used Perl to sort out some incoming data for a new exericse overnight. That's not because Perl is "better" than Java - its just that it's more appropriate for the quick, one-off data reformat that I needed.
chop;
$lyne .= $_ . " ";
$k%=3;
$k++;
print ... | 2015-01-15 |
4301 | Perl - still a very effective language indeed for extracting and reporting Perl remains a marvellous programming language for a number of applications, including quick / one off scripts which may be needed to manipulate data is ways that vary from day to day and week to week - research type work, if you like.
We manage / look after our own web server, hosting our IT training ... | 2014-09-21 |
3449 | Apache Internal Dummy Connection - what is it and what should I do with it? When the Apache HTTP Server manages its child processes, it needs a way to wake up processes that are listening for new connections. To do this, it sends a simple HTTP request back to itself. This request will appear in the access_log file ... typically from 127.0.0.1 or your server's IP address. For ... | 2011-09-19 |
2876 | 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 ... | 2010-12-04 (longest) |
2972 | Some more advanced Perl examples from a recent course I ran an extra Perl for larger projects course, single company, at the tail end of last week and into the weekend (the only gap in my diary for a few weeks!) and - as is often the case on single-company courses - I wrote some new illustrative code to show specific subjects that came up in a different ... | 2010-09-27 |
2833 | Fresh Perl Teaching Examples - part 2 of 3 Three part article ... this is part 2. Jump to part [1] or [3]
Perl is the most tremendously powerful and fully featured langauge - you've probably seen that from the first set of sample programs from last week's course, which I wrote up over the following weekend to share with a wider audience.
Here ... | 2010-06-27 (longer) |
969 | Perl - $_ and @_ Perl's a great language for special variables - variables that are set up without the programmer having to intervene and providing information ranging from the number of lines read from the current input file ($.) through the current process ID ($$) and the operating system ($^O). Other special variables ... | 2010-06-22 |
1136 | Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP When you go to board a plane at Heathrow, do the ground staff admit the passengers one at a time, ensuring that each is seated before the next boards, and sealing and re-opening the main doors between each? What a stupid and inefficient system that would be!!
The same thing applies when you're programming ... | 2009-08-31 |
1289 | Pure Perl #!/usr/bin/perl -n
s/\<.*?>//g;
@fol = split;
if (/06:44/) {
@op = @fol[1..$#fol -1];
print "@op\n";
}
I still enjoy - REALLY enjoy - filtering data and grabbing stats out of it. And what better than Perl - the Practical Extraction and Reporting Language. See ... | 2009-01-22 |
1922 | Flurinci knows Raby Lae PHP and Jeve Three more Perl Programming examples for you ...
Use of $" to produce csv data
Efficient code with map and grep
Use of implicit loop and print (-p switch)
... All from the Topicalisation and Special Variable module of our Learning to Program in Perl course.
Puzzled by the subject line? - It's a sample ... | 2008-12-03 |
1860 | Seven new intermediate Perl examples From 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, ... | 2008-11-02 (longer) |
1829 | Dont bother to write a Perl program I can - very easily - write a Perl program to process every line of an incoming data file - indeed, that's much of where Perl originated as the "Practical Extraction and Reporting Language"
Here's a short example that processes every line of a file and reports each line that includes the string PHP ... | 2008-10-10 |
1221 | Bathtubs and pecking birds I have learnt of something called the "bathtub" effect in the last year or two. A complex new product has a teething period during which support requirements are high. The requirement drops away (like - into a bathtub) as the product gets more mature, but then the curve rises again as the product ... | 2008-09-16 |
1728 | A short Perl example #!/usr/bin/perl -pa
$_ = "$F[0] $F[-2]\n";
What does that Perl code do?
Although it's very short, it's also somewhat obscure, and my delegates yesterday were not at all sure that they would wish to be given code like this to maintain ... but then they admitted that they have seen things like this around ... | 2008-07-30 |
1705 | Environment variables in Perl / use Env If I say use Env;, Perl loads in a module which allows me to access all my environment variables directly within my code, by name. If I add an explicit list of variable names as a list to the module, then only those variables will be imported. So in this example, $PATH prints nothing but $SHELL and ... | 2008-07-12 |
1704 | Finding operating system settings in Perl In Perl, you have a variety of special variables available to you ... preloaded with information in many cases. Some of their names are "special" such as $^O, $^T, $", $/, $! or $_, and others are capitalised such as @ARGV, @ISA, %INC and %ENV.
Here's a snippet of code that checks whether you're running ... | 2008-07-10 |
1508 | How not to write Perl? You can write a piece of code that resembles a Picasso ... or a piece of code that resembles a dog's dinner ... in Perl. And, rush job yesterday, I wanted to analyse a web access log file and find the number of unique visiting hosts listed. Here's what I came up with:
open (FH,"t09");
while (<FH>) ... | 2008-01-16 |
1444 | Using English can slow you right down! When you're programming and you assign a variable, that's usually a single result. For example (we're talking Perl in these examples)
$position = $record % $pagelength;
However, there are often side effects of an action which you might want to make later use of too. For example:
$postcode = ($pc ... | 2007-11-25 |
1232 | Bathtub example I wrote a few days ago about the bathtub effect. Shorten code and it becomes easier to read. Shorten it too much and it becomes silly. Here's a Perl script that I wrote to report on all lines in a log file from host computer "seal" with a status code (next to last field) over 399:
open (FH,"../access_log.xyz") or die;
while ($line = <FH>) {
if ($line =~ /^seal\s/) {
@parts = split(/\s+/,$line);
if ($parts[-2] > 399) {
print $line;
}
}
}
And ... | 2007-06-14 |
493 | Running a Perl script within a PHP page Our web site update has been progessing well over the last couple of weeks - you'll probably have notices that most of the pages changed a fortnight ago and the remaining pages are now switching over one or two at a time ... they're typically the more awkward ones!
We've been providing on site training ... | 2006-06-05 |
allhosts | Special variable $/; reading whole file to scalar |
ama | Use of $" to produce a csv output |
bcv | Use of @ARGV command line and $_ |
cl2 | Program that generates a warning |
commline | Handling the command line via @ARGV |
cv | rewrite of "dos2unix" in Perl |
delay | select to change default output |
exact | map, grep and $_ save loops |
gop | Handling the command line via Getopt. Also Usage lines. |
gop2 | Handling command line options - getopt |
hodo | Filter looking for IP addresses of computer(s) with "tea" in name |
infovars | English and short names for informational special variables |
johnny.vegas | Output from select demo |
oddgrep | Using grep and map with $_ |
paws | Progress bar / progress reports |
perltodiamond | Using the -p option on the command line |
rqu | Use of awk-like options with Perl |
sample | Filter - takes names and attempts to capitalise correctly |
shh | Counting Unique visitors in a log file |
shhh | script to run on every line of a file |
special1 | Program using a variety of special variables |
spot | Using implicit loop with -n |
spout | Special variables to control output formatting of lists |
start | BEGIN - CHECK - INIT - main code - END |
ticker | Clock, using autoflush to update screen every second |
top3 | Further use of $_ |
topic1 | Variable used many times - candidate for topicalisation |
topic2 | Topicalisation in use |
topper | File handle <> |
was | Use of command line options in Perl |
You may
download this module as a sample of our material
Regular variables in Perl.
Special variables.
Topicalization.
Special information variables and the English module.
Reading the command line in Perl.
Special variables that provide controls.
Output control variables.
Output buffering.
Input controls.
Special variables provided by Perl operations.
Error information.
Regular expression match information.
Other special variables you may come across.
Command line options.
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Ruby,
Lua,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.