http://www.perlmonks.org?node_id=302367


in reply to How's your Perl?

  1. perl -e"sub t{ \@_ }; $s='this'; *x = t $s, $s; $x[0] = 'that'; print join' ',@x;"
  2. perl -e"foo while1" or perl -e"{foo;redo}"
    1. perl -e"$foo = [{1=>1}, 'foo']; print \$foo->{1}, \$foo->[1]; "
    2. ?
  3. ?
  4. ?
  5. ?
  6. ?
  7. ?
    1. perl -e"$foo = \\1; print eval(q[$$foo]), eval(q[$$foo + 0])"
    2. ?
  8. ?
  9. ?
  10. perl -e"sub STDOUT{\*STDOUT}"
  11. Bonus perl -e"print \undef; sub foo{} goto +foo"

Replies are listed 'Best First'.
Re: Re: How's your Perl?
by hessef (Monk) on Oct 27, 2003 at 15:37 UTC
    I couldn't get :foo while1" to work on my PC (Windows, perl 5.8.0).

    I could get to work by rewriting it as "1while foo" or "1until!foo".

    My own try involved a for loop--no dice. The best I could do was "for(;;foo){};" which is slightly too long.

      I could get to work by rewriting it as "1while foo" or "1until!foo".

      That loop will stop as soon as foo() returns false. The quiz didn't specify that foo() will always return a true value, so you can't assume it.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      :(){ :|:&};:

      Note: All code is untested, unless otherwise stated

Re: Re: How's your Perl?
by xmath (Hermit) on Oct 27, 2003 at 10:31 UTC
    1. yes

    2. no and yes (perl treats while1 as one identifier)

    3a. yes

    8a. yes, cool, unexpected solution :-)   our official version uses a much much more obscure trick.. funny we never saw this one
    Try this instead: eval(q[$$foo]) ne eval(q["$$foo"])
    •Update: actually, no, the original was fine too.. even though $$foo and $$foo + 0 print differently, they don't compare numerically unequal, which is what the exercise was

    11. doesn't appear to work for me..

    % perl -e 'sub STDOUT{\*STDOUT} <STDOUT> eq <+STDOUT> or die' Died at -e line 1.

    12. yes, but why? :-)

    •Update: fixed the "try this".. initially said "!=" instead of "ne"

      12. yes, but why? :-)

      I missed this before.

      For the same reason that these give a similar error

      D:\TEMP>perl -e"sub f{'fred'} goto +f" Can't find label SCALAR(0x15d7c1c) at -e line 1. D:\TEMP>perl -e"sub f{'fred'} goto(f)" Can't find label SCALAR(0x15d7c1c) at -e line 1. D:\TEMP>perl -e"sub f{'fred'} goto f->()" Can't find label SCALAR(0x15d7c1c) at -e line 1.

      But these do not ;^)

      D:\TEMP>perl -e"sub f{'fred'} goto ~~f" Can't find label fred at -e line 1. D:\TEMP>perl -e"sub f{'fred'} goto ''.f" Can't find label fred at -e line 1. D:\TEMP>perl -e"sub f{'fred'} goto scalar f" Can't find label fred at -e line 1. D:\TEMP>perl -e"sub f{'fred'} goto ${\f}" Can't find label fred at -e line 1.

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      Hooray!

        For the same reason that these give a similar error

        Yes but why ?   :-)

        Juerd found this one originally, and while it was obviously a parser bug, it initially puzzled us how it managed to produce that reference. (Because it takes a walk through the perl source code to truly understand the issue, I made it a bonus exercise instead of a regular one)