
"There's more than one way to do it". So says the first book in our
Perl Library - Perl Programming, also known as "The Camel Book". And that eclectic collection of lots of ways of doing the same thing applies all over Perl; when I'm running
Perl Courses, I'm often asked "how do I ..." questions and have to think for a moment as to which of multiple ways I'll suggest. Such suggestions are then, of course, based on practicality, maintainability, efficiency, and the customer's standards if (s)he has any, and his / her current knowledge.
It was a
Long hot summer. And here are three lines of Perl which print out that fact, filling in the word
hot from a variable:
print "Long ",$heat," summer\n";
print "Long ".$heat." summer\n";
print "Long $heat summer\n";
In the first example, I'm passing a list of three parameters to the
print function and it's outputting them after each other. In the second, I'm using the "." operator to join three strings together and passing a single parameter to print. And in the third case, the double quote operator is being used to substitute the contents of a variable within a string.
None of these three example is particularly THE right or wrong way. But none of them is particularly exotic. I say to classes that there's usually six ways of doing things, so I should add three more ...
print join " ","Long",$heat,"summer\n";
printf "Long %s summer\n",$heat;
print "Long ${heat} summer\n";
and I could find even further examples
Full source including example lines used above -
[here]
Illustration - Guadalajara, Mexico, where I presented a
Lua Course last summer!
(written 2010-10-19, updated 2010-10-21)
Associated topics are indexed under
P205 - Perl - Initial String Handling [3770] Sample answers to training course exercises - available on our web site - (2012-06-21)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3547] Using Perl to generate multiple reports from a HUGE file, efficiently - (2011-12-09)
[3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
[2963] Removing the new line with chop or chomp in Perl - what is the difference? - (2010-09-21)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2816] Intelligent Matching in Perl - (2010-06-18)
[2798] Perl - skip the classics and use regular expressions - (2010-06-08)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1849] String matching in Perl with Regular Expressions - (2008-10-20)
[1608] Underlining in Perl and Python - the x and * operator in use - (2008-04-12)
[1195] Regular Express Primer - (2007-05-20)
[987] Ruby v Perl - interpollating variables - (2006-12-15)
[970] String duplication - x in Perl, * in Python and Ruby - (2006-12-07)
[324] The backtick operator in Python and Perl - (2005-05-25)
[254] x operator in Perl - (2005-03-22)
[31] Here documents - (2004-08-28)
56ea
Some other Articles
Expect in Perl - a short explanation and a practical example Dulwich College Preparatory, and Sevenoaks, SchoolsSetting up a matrix of data (2D array) for processing in your programSanta announcement, 5th December 2010, MelkshamLots of ways of doing it in Perl - printing out answersIncrement operators for counting - Perl, PHP, C and othersWhat will we be teaching in six years?A list of special method and attribute names in PythonHow will we present courses over the coming years?Looking forward - the next 3000