http://qs1969.pair.com?node_id=376462

tachyon has asked for the wisdom of the Perl Monks concerning the following question:

Over in Obfuscation this point came up:

C:\>type test.pl use, strict C:\>perl -MO=Deparse test.pl syntax error at test.pl line 1, near "use," test.pl had compilation errors. C:\>test.pl C:\>type test.pl use => strict C:\>perl -MO=Deparse test.pl '???', '???'; test.pl syntax OK C:\>

Now this is of course pretty arcane but what is the difference and why?

cheers

tachyon

  • Comment on When is => ne , (warning rather arcane and possibly totally useless)
  • Download Code

Replies are listed 'Best First'.
Re: When is => ne , (warning rather arcane and possibly totally useless)
by edan (Curate) on Jul 22, 2004 at 06:37 UTC

    Perhaps I'm just being thick, and there is something much more subtle going on here that I am missing, but isn't that exactly the documented difference between ',' and '=>'? From perldoc perlop (emphasis mine):

    The => digraph is mostly just a synonym for the comma operator. It's useful for documenting arguments that come in pairs. As of release 5.001, it also forces any word to the left of it to be interpreted as a string.

    So the statement 'use => strict' is a 2-element list in void context, whereas using a comma allows 'use' to be interpreted as a keyword, thus the syntax error...

    --
    edan

      I agree. When the string is made explicit, it does not change the result:

      perl -MO=Deparse -e "'use', 'strict'" '???', '???'; -e syntax OK perl -MO=Deparse -e "'use'=> 'strict'" '???', '???'; -e syntax OK
Re: When is => ne , (warning rather arcane and possibly totally useless)
by Zaxo (Archbishop) on Jul 22, 2004 at 04:30 UTC

    I suspect that use is stringified by a fat comma before it is recognised as a keyword, I haven't yet figured out how to test that theory.

    After Compline,
    Zaxo

      Yes, that's exactly what's happening:
      % perl -le 'print for f(); sub f {use => strict}' use strict
Re: When is => ne , (warning rather arcane and possibly totally useless)
by BrowserUk (Patriarch) on Jul 22, 2004 at 04:28 UTC

    When it's the same as

    P:\test>perl -e"'fred' => 1;"

    A hash element pair in a void context?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

      There is no "hash" involved here. It's merely a list.

      perl -MO=Deparse -e "'moo', 'oom', 'zoop'" '???', '???', '???'; -e syntax OK

        I've been reading to much Perl 6 stuff where Pairs are distinct entities. Your right. It's just a 2 element list in a void context which is being optimised away. Hence the '???'.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
Re: When is => ne , (warning rather arcane and possibly totally useless)
by dragonchild (Archbishop) on Jul 22, 2004 at 12:43 UTC
    I think part of the boggling has to do with the fact that 'strict' is a legal bareword if strictures are off. I had to think through that one when I looked at this one. (Nice conundrum, btw. ++!) You see "use strict" in some fashion and that bit is flipped in your head, regardless of the fact that the mess you're considering is the "use strict" declaration itself. So, I was applying strictures (specifically strict 'refs') without truly verifying that strictures were in force.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested