in reply to CarTalk Puzzler

how about some golf? (yes, I know it's cheating :P )
print sqrt==int(sqrt)?1:0for(1..2e4);
BTW: As a slight drift, how did I manage to get away without a space before the 'for'?
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: CarTalk Puzzler
by Aristotle (Chancellor) on Nov 18, 2005 at 13:51 UTC
    print sqrt==int(sqrt)?1:0for 1..2e4;

    :-)

    You got away with it because perl is looking for an expression; when the thing it then sees starts with a digit, it can't be anything but a literal number. So the parser eats as many characters as can be part of a literal number and then stops. For some fun, insert an x right after the 0 and watch what happens; or try any number of underscores.

    Makeshifts last the longest.

      Adding underscore doesn't add much fun. Adding an 'x' does:
      $ perl -le 'print 0for 1, 2, 3' 0 0 0 $ perl -le 'print 0_for 1, 2, 3' 0 0 0 $ perl -le 'print 0xfor 1, 2, 3' 15
      Perl --((8:>*

        True; the underscore is only interesting after trying x, and mostly for looking at the B::Deparse output.

        Makeshifts last the longest.

Re^2: CarTalk Puzzler
by Perl Mouse (Chaplain) on Nov 18, 2005 at 12:38 UTC
    print 0+sqrt!~/\./for 1..2e4
    As a slight drift, how did I manage to get away without a space before the 'for'?
    Because you only need spaces if the concatenation of the two tokens creates a longer leading token. But in Perl, no token starts with '0f', hence perl knows '0' is a token, and 'for' another.
    Perl --((8:>*
        
      print 0+sqrt!~/\./for 1..2e4
      very cool lateral thinking.

      cheers for the replies re the space thing. I never really give any thought to the parser. How do you pick this stuff up without delving into the internals? (which I don't think I'm ready for)

      ---
      my name's not Keith, and I'm not reasonable.
        How do you pick this stuff up without delving into the internals?
        That's how most of the important lexers/parsers work: they find the next token in a greedy way. That's why in most languages white space is only needed to separate tokens if concatenating them causes a different (and unwanted) way of parsing.

        It's nothing specific to Perl, nor do you need to know anything about the internals.

        Perl --((8:>*