I was wondering if other people here found it as interesting as my co-workers and I do, to answer "What will this print?" questions...Ya'know those small snippets of code that, when run, output something interesting and unexpected...Generally the output is a big hint as to how it works, so running the snippet before guessing is half-cheating :)

Maybe some of you would be willing to share some of your favorites, or maybe this has come up more than I thought at the Monastery and you could point me to some nice nodes?

Here's a few recent ones that we've enjoyed. The first of which is similar to something I saw on Perlmonks in the past (I don't recall the node).

WWTP #1

package one; sub two { print "In one::two\n"; } package main; $one = "main"; sub main { return "one: " }; sub two { print "In main::two\n"; } $one->two(); "one"->two();

WWTP #2

$one = "two"; $two = "three"; $three = "four"; $four = "five"; $five = "six"; print $$$one . "\n"; print $$${"one"} . "\n";

WWTP #3

$one = "two"; %$one:: = %::; print %$one . "\n"; %$one = %::; print %$one . "\n";


Staunch

Replies are listed 'Best First'.
Re: What will this print?
by Abigail-II (Bishop) on Mar 21, 2003 at 18:58 UTC
    Ehm, what's the puzzle?

    In WWTP #1, we have two method calls. The first, $one->two(); calls two() in whatever package name is in $one, in this case, main, so main::two is called. The second print just calls one::two. You're not using @_, so what's passed as an argument isn't relevant.

    In WWTP #2, you are using soft references. $one is two, $two is three and $three is four. So, $$$one (which can also be written as $$${"one"}) is four.

    In WWTP #3, you first assign the %:: (which is the main stash) to %$one::, the stash of two namespace. Then you print the hash %$one (no relation to %$one::) in scalar context. This results in 0, because %$one is empty. Then you assign %:: to %$one, and you print that hash in scalar context. A non-empty hash in scalar context gives you a string with the number of used buckets and the number of allocated buckets, separated by a slash, as documented in perldata.

    Abigail

      I was certainly not trying to represent those questions as extremely difficult and certainly nothing that's going to throw you off :)

      I am really hoping that others will post some more WWTPs. I find these type of questions a fun way to learn and exercise my Perl knowledge, and hope others might as well.

      I appreciate your concise descriptions.

      Thanks,
      Staunch

Re: What will this print?
by demerphq (Chancellor) on Mar 21, 2003 at 19:53 UTC
Re: What will this print?
by grantm (Parson) on Mar 21, 2003 at 19:56 UTC

    I liked this handy little debugging aid from mjd's Perl Hardware Store talk:

    @x = (0, 'one', ' ', 3); $"=')('; print "(@x)";

    And here's one that looks kind of obvious, but has a subtlety or two:

    my $i = 0; sub inc { ++$i } foreach (1..10) { print "$_\n" if(inc == 3 .. inc == 8) }

    Update:

    And here's one that just bit me. It took a few minutes before I realised what was going on:

    my $animal = 'pig'; foreach $animal ('cat', 'dog', 'horse') { last if $animal eq 'dog'; } print "$animal\n";
      my $animal = 'pig'; foreach $animal ('cat', 'dog', 'horse') { last if $animal eq 'dog'; } print "$animal\n";
      I had to laugh out loud at this one. I've used this meme so many times in my first real language, Pascal (where it actually does what you expected), and hated it every single time, that I picked it up from a mile away against the wind. The marks of one's upbringing.. :)

      Makeshifts last the longest.

Re: What will this print?
by sschneid (Deacon) on Mar 21, 2003 at 18:32 UTC
    Thanks, staunch! I agree that this a really fun way to learn something. I won't say which one, but I'll admit that I scratched my head on one of them to realize why it was printing what it was.

    -s.