"There's more than one way of doing it" ... the mantra of Perl, which is both a blessing and a curse.
It's a blessing because the programmer has a rich array of tools to hand and can always find a solution, making this ons of the most flexible languages around, and making it quick and easy for the experienced programmer to get to a working model very quickly.
But it's a curse because it means that it's too easy to write code that the newcomer finds very hard to follow, or indeed to write code that's hard for anyone to follow later on.
From last week's Perl course -
[here] is an example showing five different ways of looping through each member of a list and displaying its contents ... I've shown three flavours of
while loop, with different terminator conditions, a
for loop using one of those teminations, and a
foreach loop which dosn't use the element number at all. Using variations on the same theme, I could have provided two more
for loop examples, and three more using
foreach rather than
for. That's before you start looking at
until loops, or
do ... while type structures
For the really bizarre, you could also use labelled blocks:
$p = 0;
jumper:
{
print "Have a drink for me in $places[$p]\n";
redo jumper unless (++$p >= scalar @places);
}
And I won't even mention that Perl also has
three flavours of
goto.
Our
Perl courses teach
good programming practice as well as the philosophy and techniques of the language. It's our obective to teach you to write code well that's robust and can be easily read and maintained later on.
(written 2012-12-03, updated 2013-01-01)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P208 - Perl - Lists [28] Perl for breakfast - (2004-08-25)
[140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
[230] Course sizes - beware of marketing statistics - (2005-02-27)
[240] Conventional restraints removed - (2005-03-09)
[355] Context in Perl - (2005-06-22)
[463] Splitting the difference - (2005-10-13)
[560] The fencepost problem - (2006-01-10)
[622] Queues and barrel rolls in Perl - (2006-02-24)
[762] Huge data files - what happened earlier? - (2006-06-15)
[773] Breaking bread - (2006-06-22)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[968] Perl - a list or a hash? - (2006-12-06)
[1304] Last elements in a Perl or Python list - (2007-08-16)
[1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
[1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
[1828] Perl - map to process every member of a list (array) - (2008-10-09)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[1918] Perl Socket Programming Examples - (2008-12-02)
[2067] Perl - lists do so much more than arrays - (2009-03-05)
[2226] Revision / Summary of lists - Perl - (2009-06-10)
[2295] The dog is not in trouble - (2009-07-17)
[2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
[2813] Iterating over a Perl list and changing all items - (2010-06-15)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
[3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
[4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
Some other Articles
With us for a year already!Santa Trip - Melksham Railway Development Group, 2012Building an object based on another object in PerlRun other processes from within your Perl programLots of ways of doing the same thing in Perl - list iterationA long overdue meeting - a steeping stone towards coordinate transport user inputsAgenda for Wiltshire Link Travel groups meeting, Saturday 1st December 2012Lisa is home today ...Whether you have programmed before or not, we can teach you PythonMultiple identical keys in a Python dict - yes, you can!