I was answering a question posted to one of the myriad of Perl mailing lists a few moments ago, and was reminded of this interesting point.
We're all told (or do the telling of) "use strict always!" and "barewords are bad". But the motivation for why barewords are bad might not be clear.
Back in the Perl3 days, when barewords were added, I came up with this interesting little puzzle. See if you can solve it. Don't post your answers without hiding them somehow (readmore, black-on-black, or some other way).
The Puzzle
Presuming no "use strict" (or the days of Perl3), why does:
@days = (sun,mon,tue,wed,thu,fri,sat);
work OK, but
@months = (jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec);
fail?
Re: Why use strict is good, and barewords are bad
by cog (Parson) on Apr 21, 2005 at 16:21 UTC
|
I think I got it...
Am I right? | [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by dragonchild (Archbishop) on Apr 21, 2005 at 16:26 UTC
|
| [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by nimdokk (Vicar) on Apr 22, 2005 at 12:39 UTC
|
| [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by 5mi11er (Deacon) on Apr 21, 2005 at 16:31 UTC
|
Because one of those is a reserved word...
-Scott | [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by muba (Priest) on Apr 21, 2005 at 17:04 UTC
|
Without checking the other three replies, I think I know why.
It appears I'm right :)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Why use strict is good, and barewords are bad
by Booger (Pilgrim) on Apr 21, 2005 at 18:12 UTC
|
The @months array fails to properly store the barewords because...
Heh. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
Until a new reserved word is added to the language. I could see dec used as a decrement operation equivilent to $foo = $bar - 1;. Or maybe you have a module that imports an apr() sub into your namespace for calculating Annual Precentage Rates. Or maybe something completely different.
The point is, you can't guarentee that any of those won't take a different meaning at some point in the future, so you should just avoid using them as barewords.
"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
LOL:). One more to my colllection of "expect the unexpected".
My 1st guess out of
jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
was "may", second - "dec", and only 3d - "oct" (note that I don't Perl).
Imagine:
may $foo == $bar;
then $awk = sep($foo, $bar, 18) unless oct(18) eq int("2".dec("2.00000
+0)")
:-).
| [reply] [Watch: Dir/Any] [d/l] |
|
Or maybe you have a module that imports an apr() sub into your namespace for calculating Annual Precentage Rates.
As long as subs (or new buildins) without arguments return a string with the same name as they have, all will be well. I mean, if oct were to be defined that oct() eq "oct", the OPs example would not have been a problem.
| [reply] [Watch: Dir/Any] |
|
|
Re: Why use strict is good, and barewords are bad
by ryantate (Friar) on Apr 21, 2005 at 20:06 UTC
|
Guess without reading others:
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by markmoon (Deacon) on Apr 22, 2005 at 00:56 UTC
|
Without cheating... I think:
--
@a = ("a".."z"," ","-","\n");foreach $b (
12,0,17,10,24,12,14,14,13,26,8,18,26,0,26,
22,0,13,13,0,27,1,4,26,15,4,17,11,26,7,0,
2,10,4,17) {print $a[$b]};print $a[28];
| [reply] [Watch: Dir/Any] [d/l] |
Re: Why use strict is good, and barewords are bad
by cog (Parson) on Apr 22, 2005 at 10:31 UTC
|
Let's make matters worse
Anyone has other ideas, perhaps to complete the list? :-)
(OK, I admit it... that was really stupid...) | [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
JAN: Java Alert Notification
FEB: Free Each Binary
MAR: Must Always Run
APR: Average Programmer Rate
MAY: May Abort Yearly
JUN: Jump Until Near
JUL: Join Unique List
AUG: Accelerate Ultimate Game
SEP: Shift Extra Parameter
OCT: Open, Close and Truncate
NOV: Next Old Vector
DEC: Do Expected Calculation
| [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by jhourcle (Prior) on Apr 22, 2005 at 00:38 UTC
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
I did indicate "in the days of perl3". Ampersandless subroutine calls weren't permitted way back when. Oh, how quickly you young'uns forget. {grin}
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by johndageek (Hermit) on Apr 21, 2005 at 17:16 UTC
|
checked it out. found the answer. It agrees with the above.
| [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by nimdokk (Vicar) on Apr 21, 2005 at 17:36 UTC
|
Nice, checked what I thought my answer was and I was right (pretty much, went a bit too far, but good enough :) | [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by JanneVee (Friar) on Apr 23, 2005 at 20:34 UTC
|
Actually both my syntaxhighlighting editors caught the error and highlighted the bareword that failed. Some people would argue that syntax highlighting makes one lazy, but I find it an productivity enhancing tool. | [reply] [Watch: Dir/Any] |
Re: Why use strict is good, and barewords are bad
by TedPride (Priest) on Apr 22, 2005 at 10:09 UTC
|
oct is a reserved word. I notice that the following works fine, however:
@months = qw/jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec/;
I always use this format or enclose the text inside quote marks. | [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
|
|