Perl is a stunningly customizable language. Its original motto "There's more than one way to do it" (TMTOWTDI or TIMTOWTDI, pronounced Tim Toady) makes it a great language for programmers to write very creative code. The flip side of this is that it makes it very hard for someone else to read them. This post thankfully is not about the pros and cons of this motto. Here I show you some code that demonstrates how versatile perl code can be!
In perl you can write a word without any single or double quotes around it and assign it to a variable.
As long as
TheBar is not a declared variable (or file handle or keywords, etc.) perl will assume it is a string (an english word to be precise). These are called
barewords in perl lingo. Of course if you think this is not a good idea perl gives you the flexibility to turn the feature on and off as you please with
use strict q(subs) and
no strict q(subs) respectively.
In perl a quote can be a quote or anything else you want it to be!
Suppose, you want to put three words (e.g. unix commands) in a array. In most languages the code will look something like
commands = ['ls', 'pwd', 'who']; In perl there are many ways of doing this:
- No quotes around the words, less typing.
- No commas separating the words even less typing!
()- more common array notation
We can do get it all for the price of that little
Enter the q operator
Using the q operator you can decide what your quotes for string literals should be and how do you want perl to interpret the literal.
And yes we can be both creative and confusing! Can you guess what will be in $output?
TIMTOWTDI motto is opposite of Zen of Python "There should be one — and preferably only one — obvious way to do it".