I'm running a
Perl Course this week ... a small group, so we can look at some very interesting constructs that I wouldn't normally cover / consider on a public course.
Comment out a block of code with an impossible condition
if (1 == 0) { ....
This is a great way to temporarily comment out a whole block of your code if you want to supress it for a while.
Toggle a variable between two values
$n = 3 - $n;
If $n was one before the statement, it becomes 2, and vice versa. The constant you use (3 in this case) is simply the sum of the two values you wish to toggle between.
Output an integer in binary
printf ("%b",$value);
sprintf and printf in Perl support the "b" formatter for binary - in addition to the more common "o" for Octal, "d" for decimal and "x" for hexadecimal.
All of these techniques together ...
# Some Perl Tricks
$n = 1;
if (1 == 0) {
print "This is a comment";
print "And so is this"; }
for ($k=1;$k<20;$k+=3) {
$n = 3 - $n;
printf ("%05b %2d %2d\n",$k,$n,$k); }
And when we run that:
grahamellis$ perl ppa
00001 2 1
00100 1 4
00111 2 7
01010 1 10
01101 2 13
10000 1 16
10011 2 19
grahamellis$
(written 2007-08-21)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P207 - Perl - File Handling [12] How many people in a room? - (2004-08-12)
[114] Relative or absolute milkman - (2004-11-10)
[255] STDIN, STDOUT, STDERR and DATA - Perl file handles - (2005-03-23)
[616] printf - a flawed but useful function - (2006-02-22)
[618] Perl - its up to YOU to check your file opened - (2006-02-23)
[702] Iterators - expressions tha change each time you call them - (2006-04-27)
[867] Being sure to be positive in Perl - (2006-09-15)
[1416] Good, steady, simple example - Perl file handling - (2007-10-30)
[1442] Reading a file multiple times - file pointers - (2007-11-23)
[1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
[1709] There is more that one way - Perl - (2008-07-14)
[1841] Formatting with a leading + / Lua and Perl - (2008-10-15)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1861] Reactive (dynamic) formatting in Perl - (2008-10-31)
[2233] Transforming data in Perl using lists of lists and hashes of hashes - (2009-06-12)
[2405] But I am reading from a file - no need to prompt (Perl) - (2009-09-14)
[2818] File open and read in Perl - modernisation - (2010-06-19)
[2821] Chancellor George Osborne inspires Perl Program - (2010-06-22)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[3326] Finding your big files in Perl - design considerations beyond the course environment - (2011-06-14)
[3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
[3830] Traversing a directory in Perl - (2012-08-08)
[3839] Spraying data from one incoming to series of outgoing files in Perl - (2012-08-15)
P207 - Perl - File HandlingP202 - Perl Fundamentals [184] MTBF of coffee machines - (2005-01-20)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[1726] Hot Courses - Perl - (2008-07-28)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1946] Variable Types in Perl - (2008-12-15)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3329] Perl from basics - (2011-06-20)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
Some other Articles
Filtering and altering Perl lists with grep and mapTwo years of campaigning for a train serviceBusiness travel by train in the USATratum TechnologiesSome one line Perl tips and techniquesWhat do people look for on a hotel web site?Callbacks - a more complex code sandwichDates for Easter - 2008 to 2015Good to be homeTroy, up state New York