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

Is this another reduce bug or am I missing something?

#! perl -slw use strict; use List::Util qw[ reduce ]; my $s = 'this is a test'; reduce{ print "$a:$b"; $b; } split ' ', $s; print "\n---\n"; my @p; reduce{ print "$a:$b"; push @p, [ 1, 2 ]; $b; } split ' ', $s; print "\n---\n"; my @q; reduce{ print "$a:$b"; push @q, [ $a, 2 ]; $b; } split ' ', $s; print "\n---\n"; my @r; reduce{ print "$a:$b"; push @r, [ $a, $b ]; $b; } split ' ', $s; __END__ c:\test>junk3 this:is is:a a:test --- this:is is:a a:test --- this:is is:a a:test --- this:is Use of uninitialized value in concatenation (.) or string at c:\test\j +unk3.pl line 31. :a Use of uninitialized value in concatenation (.) or string at c:\test\j +unk3.pl line 31. :test

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re: Another reduce bug?
by wind (Priest) on Aug 16, 2007 at 02:31 UTC

    You're not missing anything. I observe the same bug. The following two variations make the problem disappear.

    Using pure perl version of List::Util

    #! perl -slw use strict; BEGIN {$List::Util::TESTING_PERL_ONLY = 1}; use List::Util qw[ reduce ]; my $s = 'this is a test'; my @r; reduce { print "$a:$b"; push @r, [ $a, $b ]; $b; } split ' ', $s;

    Or passing an array instead of the split:

    #! perl -slw use strict; use List::Util qw[ reduce ]; my $s = 'this is a test'; my @s = split ' ', $s; my @r; reduce { print "$a:$b"; push @r, [ $a, $b ]; $b; } @s;
    - Miller

      Ha! Thanks for the confirmation. It is a strange one. Following on from your use an array solution, I tried the ol' put it in a list context trick--which is probably a erroneous name, but it serves-- and voila. That also fixes it. Maybe an interation between two different optimisations? Time for a perlbug I think.

      my @r; reduce{ print "$a:$b"; push @r, [ $a, $b ]; $b; } () = split ' ', $s;

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I expect that to make reduce() loop over an empty list and, for me, that is what it does. So, at least in my environment, I don't see how that qualifies as "fixing".

        - tye        

Re: Another reduce bug? (noinc)
by tye (Sage) on Aug 16, 2007 at 02:32 UTC

    I'd guess a ref-counting bug, yes (which includes bugs in use of mortalization / FREETMPS).

    - tye