deibyz has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I've noticed a weird behavior of a very simple loop with a warn statement.

#!/usr/bin/perl use strict; use warnings; for my $f (qw(a b)){ print $f; warn "."; }

This code runs as expected an produces thew following output:

. at test.pl line 7. . at test.pl line 7. ab

However, if I don't put an argument to warn:

#!/usr/bin/perl use strict; use warnings; for my $f (qw(a b)){ print $f; warn ; }

I get the following output:

Warning: something's wrong at test.pl line 7. Warning: something's wrong at test.pl line 7. a1

Notice the "1" instead of "b".

Any ideas about this behavior?

My perl version:

$ perl -v This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

Replies are listed 'Best First'.
Re: Strange warn behavior in loop
by wazoox (Prior) on Jul 16, 2007 at 12:09 UTC

    I've tried on some of the systems I have at hand, and it looks like a v5.8.8 bug :

    SystemPerl versioncorrect
    Linux Slackware 11.0 v5.8.8 i486-linuxNo
    Linux Debian Sargev5.8.4 i386-linux-thread-multiYes
    Linux Debian Etchv5.8.8 i486-linux-thread-multiNo
    Mac OS X 10.4v5.8.6 darwin-thread-multi-2levelYes
    IRIX 6.5.22 v5.6.1 irix-n32Yes
      I've tried on some of the systems I have at hand, and it looks like a v5.8.8 bug :

      Confirmed under the Redmondish side of the world, too:

      C:\temp>perl -e "for (2,2) {print;warn}" Warning: something's wrong at -e line 1. Warning: something's wrong at -e line 1. 21

      Note that I wanted to further simplify the test code, but the bug doesn't seem to show up with a c<for> statement modifier:

      C:\temp>perl -e "print,warn for 2,2" Warning: something's wrong at -e line 1. Warning: something's wrong at -e line 1. 22

      BTW: what if $@ is set?

      C:\temp>perl -e "$@='all your base are belong to us';for (2,2) {print; +warn}" all your base are belong to us ...caught at -e line 1. all your base are belong to us ...caught ...caught at -e line 1 +. 21

      Nope, the bug is still there...

Re: Strange warn behavior in loop
by moritz (Cardinal) on Jul 16, 2007 at 11:21 UTC
    On my Debian Etch I get the incorrect result a1 from perl shipped with Debian.

    $ perl -v This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

    But it seems to be fixed in 5.9.5 (correctly produces ab):

    This is perl, v5.9.5 DEVEL31566 built for i686-linux

Re: Strange warn behavior in loop
by rpanman (Scribe) on Jul 16, 2007 at 11:20 UTC
    Doesn't occur for perl v5.8.7 built for i486-linux-gnu-thread-multi.
    Warning: something's wrong at test.pl line 7. Warning: something's wrong at test.pl line 7. ab
    perl588delta says the following:

    There has been a fair amount of refactoring of the C source code, partly to make it tidier and more maintainable. The resulting object code and the perl binary may well be smaller than 5.8.7, in particular due to a change contributed by Dave Mitchell which reworked the warnings code to be significantly smaller. Apart from being smaller and possibly faster, there should be no user-detectable changes.

    ...maybe that's introduced this weird behaviour...
Re: Strange warn behavior in loop
by johngg (Canon) on Jul 16, 2007 at 11:06 UTC
    The strange behaviour doesn't occur with perl 5.8.4 built for sun4-solaris.

    Warning: something's wrong at spw626811 line 7. Warning: something's wrong at spw626811 line 7. ab

    I can't think what could be causing that.

    Cheers,

    JohnGG

Re: Strange warn behavior in loop
by betterworld (Curate) on Jul 16, 2007 at 13:40 UTC
    This reminds tinita and me of this bug.

    In both cases the loop variable is set to "1" by some mysterious force, and in both cases warn must be called without arguments to trigger the bug.

    Update: I've added the code from that bug report under the readmore tags.

Re: Strange warn behavior in loop
by ikegami (Patriarch) on Jul 16, 2007 at 14:41 UTC
    5.8.8 on Windows (ActivePerl build 820) also fails (prints a1), so it's not a platform issue.