in reply to What will this print?

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";

Replies are listed 'Best First'.
Re^2: What will this print?
by Aristotle (Chancellor) on Mar 31, 2003 at 09:23 UTC
    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.