Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Why use strict is good, and barewords are bad

by merlyn (Sage)
on Apr 21, 2005 at 16:14 UTC ( [id://450080]=perlmeditation: print w/replies, xml ) Need Help??

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?

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: Why use strict is good, and barewords are bad
by cog (Parson) on Apr 21, 2005 at 16:21 UTC
Re: Why use strict is good, and barewords are bad
by dragonchild (Archbishop) on Apr 21, 2005 at 16:26 UTC
Re: Why use strict is good, and barewords are bad
by nimdokk (Vicar) on Apr 22, 2005 at 12:39 UTC
    Reminds me of the old (I suppose) joke:

    Why do programmer's get Halloween and Christmas mixed up?

    Answer: OCT 31 = DEC 25

    :-)

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

Re: Why use strict is good, and barewords are bad
by muba (Priest) on Apr 21, 2005 at 17:04 UTC
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.

      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.

        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)")

        :-).
        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.
Re: Why use strict is good, and barewords are bad
by ryantate (Friar) on Apr 21, 2005 at 20:06 UTC
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];
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...)

      I'd had the same thought off and on since leaving from work yesterday. You did much better than I'd been able. I was trying to use them all as TLA's, but hadn't come up with a single good one yet, but did figure I'd steal the APR idea from way up above.

      Just Another Nitwit
      Match Against Regex
         Pretty lame, and those are my best so far...

      -Scott

        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
        
Re: Why use strict is good, and barewords are bad
by jhourcle (Prior) on Apr 22, 2005 at 00:38 UTC

    But the first one doesn't work okay all the time.

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.

    Enjoy!
    Dageek

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 :)
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.
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.
      Of course it does. You're quoting a single word (which happens to have 47 chars :-)

      @months is ending up with one single element O:-)

      You're not running this with warnings enabled, I'm sure.

      And qw is an addition from somewhere in Perl5's history.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://450080]
Approved by Corion
Front-paged by gmax
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-19 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found