During courses, I end up writing a lot of short demonstrations to show particular features of a language - this week, it's a
Perl Programming Course so those examples are in Perl.
Some interesting Perl facts ...
a) The
=> operator can be used to replace the
, ("the equals, greater than can be used to replace the comma")
b)
chop and
chomp have the same effect on a line of text just read in from STDIN (which usually means "read from the keyboard")
c) It's a good idea to take commonly repeated pieces of code and save them in a block - called a
sub or subroutine in Perl - so that you don't have to keep repeating yourself AND so that you can present your user with a consistent interface
The programs I come up with are odd / quirky / memorable and if you look back at them later will leave the new reader scratching his or her head - but the key word is
memorable. Here's the program that went some of the way to explaining the key perl facts above:
sub getui {
print "$_[0] - ";
chomp ($rv = <STDIN>);
return $rv;
}
print "How kids yagot? ";
chomp($nk = <STDIN>);
print "And how many goldfish? ";
chop($ng = <STDIN>);
$nz = getui("how many zebras?");
print "Actimel $nk pounds\n";
print "Cream Cheese ",$nk," kiddoes\n";
print "Milk $nk pounds\n";
print "Yoghurt "=>$ng=>" goldfish\n";
print "Bananas ",$nz," zebras\n";
Learning in this way is not only useful, but fun! If you're looking for the dates of the next public Perl course ... as I write (early October 2008) the next course starts on 27th October, and if you're reading this later on the archive and have missed that start date, you can find what's coming up
here in our index of courses. I look forward to helping many more people (such as yourself, perhaps?)
enjoy learning Perl!
Sample Output:
Dorothy:p1 grahamellis$
perl tootoo
How kids yagot?
3
And how many goldfish?
7
how many zebras? -
4
Actimel 3 pounds
Cream Cheese 3 kiddoes
Milk 3 pounds
Yoghurt 7 goldfish
Bananas 4 zebras
(written 2008-10-08)
Associated topics are indexed under
P202 - Perl Fundamentals [3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3329] Perl from basics - (2011-06-20)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[1946] Variable Types in Perl - (2008-12-15)
[1726] Hot Courses - Perl - (2008-07-28)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[184] MTBF of coffee machines - (2005-01-20)
P209 - Subroutines in Perl [3066] Separating groups of variables into namespaces - (2010-11-24)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
[1921] Romeo and Julie - (2008-12-04)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[969] Perl - $_ and @_ - (2006-12-07)
[775] Do not duplicate your code - (2006-06-23)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[531] Packages in packages in Perl - (2005-12-16)
[357] Where do Perl modules load from - (2005-06-24)
[308] Call by name v call by value - (2005-05-11)
[96] Variable Scope - (2004-10-22)
P211 - Perl - Hashes [3451] Why would you want to use a Perl hash? - (2011-09-20)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3106] Buckets - (2010-12-26)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1856] A few of my favourite things - (2008-10-26)
[1705] Environment variables in Perl / use Env - (2008-07-11)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[968] Perl - a list or a hash? - (2006-12-06)
[930] -> , >= and => in Perl - (2006-11-18)
[738] (Perl) Callbacks - what are they? - (2006-05-30)
[386] What is a callback? - (2005-07-22)
[240] Conventional restraints removed - (2005-03-09)
Some other Articles
Caen Hill and Olivers CastleDont bother to write a Perl programPerl - map to process every member of a list (array)What a shockPerl - Subs, Chop v Chomp, => v ,Question Mark - Colon operator (Perl and PHP)Which is your best hotel room?FSB - an update.Claverton PumpAutumn