in reply to How's your Perl?

There's a bunch of spoilers here so click readmore to read them.
  • 1. I knew it was a trick using @_'s aliasing but I didn't pick up the glob assignment until I saw what BrowserUK did.
  • 2. {foo;redo}
  • 3a. $foo=*foo;$foo->[1]=$foo->{1}=\undef
  • 3b. I started fiddling with $foo=[(\!1)x2];*{"$foo"}=sub{!aye}' but was stopped by perl complaining that $foo was an array reference. I just wanted it to call the function with the name of its strinfied reference but I didn't see an immediate way around it.
  • 4. dump
  • 5. sub foo { my $x if 0 }
  • 6. $x = \1; $y = "$x"; $$y = 2
  • 7. $foo=*STDOUT{IO}
  • 8. I'm not sure how to read this. It is invalid to assume that either post increment would occur before each other - this is one of the cases where "undefined execution order" really comes into play. Now obviously, the actual order is defined by the optree which is defined by the implementation in which the first post increment (lexically) is going to execute first. So in which light am I supposed to solve this? The fiddly bits of the implementation (which in this case I'm not sure anyone is supposed to expect anything in particular) or the implementation's promise which is unspecified?
    Update 1*foo=$|;$foo=1 This answer assumes things about perl that I'm damned if I'll admit that this is a fair question. This particular behaviour is officially undefined (as to much of the rest of perl which is undefined by ommision).
  • 9a. $0=1;$foo=0
  • 9b. I'm stumped.
  • 10. I didn't see how to do this in twenty-six characters but here's a forty-five character solution: $"=::;${"@{[map 56922&2**$_?'y':x,0..15]}"}=1. I went on to start playing with ORing the bittests into the string value 'x' and got bored. print "@{[map(q[x]|q[].!(56922&2**$_),0..15)]}"
  • 11. I got tired and while I'm still probably stumped, I don't have the initiative anymore.

Replies are listed 'Best First'.
Re: Re: How's your Perl?
by xmath (Hermit) on Oct 28, 2003 at 18:50 UTC
    re 8: perl has a fixed evaluation order (L->R for most operators, including eq), so you can use that. That it's not officially defined/documented is ofcourse of no concern :-)
      I've now see the answer (and its obvious) for #8 and in that rare case the evaluation order doesn't actually matter since it won't affect anything but if it were anything else then I'd have to call shenanigans on you for the trick question since you'd only be testing on the implementation and not the specified language.
        you'd only be testing on the implementation and not the specified language
        Joke right? specified language? perl? ;-)

        in any case... many other exercises (5, 7, 9a, 9b, 11, probably 10 too) also rely on undefined behavior, or at least their official answers do. It's considered acceptable for obfu and bizarre exercises :-)