Main Content
Some one line Perl tips and techniques Archive - Originally posted on "The Horse's Mouth" - 2007-08-21 13:52:46 - Graham Ellis
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$
Some other articles
P207 - File Handling in Perl Spraying data from one incoming to series of outgoing files in Perl Traversing a directory in Perl Dark mornings, dog update, and Python and Lua courses before Christmas Finding your big files in Perl - design considerations beyond the course environment Fresh Perl Teaching Examples - part 2 of 3 Chancellor George Osborne inspires Perl Program File open and read in Perl - modernisation But I am reading from a file - no need to prompt (Perl) Transforming data in Perl using lists of lists and hashes of hashes Reactive (dynamic) formatting in Perl Seven new intermediate Perl examples Formatting with a leading + / Lua and Perl There is more that one way - Perl stdout v stderr (Tcl, Perl, Shell) Reading a file multiple times - file pointers Good, steady, simple example - Perl file handling This article This article Being sure to be positive in Perl Iterators - expressions tha change each time you call them Perl - its up to YOU to check your file opened printf - a flawed but useful function STDIN, STDOUT, STDERR and DATA - Perl file handles Relative or absolute milkman How many people in a room? P207 - File Handling in Perl Spraying data from one incoming to series of outgoing files in Perl Traversing a directory in Perl Dark mornings, dog update, and Python and Lua courses before Christmas Finding your big files in Perl - design considerations beyond the course environment Fresh Perl Teaching Examples - part 2 of 3 Chancellor George Osborne inspires Perl Program File open and read in Perl - modernisation But I am reading from a file - no need to prompt (Perl) Transforming data in Perl using lists of lists and hashes of hashes Reactive (dynamic) formatting in Perl Seven new intermediate Perl examples Formatting with a leading + / Lua and Perl There is more that one way - Perl stdout v stderr (Tcl, Perl, Shell) Reading a file multiple times - file pointers Good, steady, simple example - Perl file handling This article This article Being sure to be positive in Perl Iterators - expressions tha change each time you call them Perl - its up to YOU to check your file opened printf - a flawed but useful function STDIN, STDOUT, STDERR and DATA - Perl file handles Relative or absolute milkman How many people in a room? P202 - Perl Fundamentals Learning to program - variables and constants BODMAS - the order a computer evaluates arithmetic expressions Perl functions such as chop change their input parameters What order are operations performed in, in a Perl expression? Perl - making best use of the flexibility, but also using good coding standards Perl from basics Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. AND and OR operators - what is the difference between logical and bitwise varieties? Object Orientation in an hour and other Perl Lectures Different perl examples - some corners I rarely explore Are you learning Perl? Some more examples for you! Variable storage - Perl, Tcl and Python compared Variable Types in Perl Perl - Subs, Chop v Chomp, => v , Hot Courses - Perl Question on division (Java) - Also Perl, PHP, Python ... This article Getting rid of variables after you have finished with them MTBF of coffee machines