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

Hi monks,

A bit of background: I'm using Filter::Simple to inline a couple of methods for speed - I replace a method call with a do block. I started having a really weird problem -  return inlinesub(); and my $ret = inlinesub(); return $ret; would behave differently, even though the function was called in scalar context. I've drilled it down to a small example:
use strict; sub bar{ if (1){ return do { #do block will return 1 if (1){ 1; } 1; } ; } } sub foo{ if (1){ return do { #do block will return 1 1; } ; } } print ( "foo said '".foo()."'\n"); print ( "bar said '".bar()."'\n");
output:
localhost:~# perl /tmp/z.pl foo said '1' bar said '' localhost:~# perl -v This is perl, v5.8.8 built for i486-linux-gnu-thread-multi Copyright 1987-2006, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. localhost:~#
This is all on debian - I get the same behaviour in stable (5.8.8-7) and unstable (5.8.8-11.1) - am I doing something wrong, or is this a bug? Thanks!

Replies are listed 'Best First'.
Re: Strange behaviour when returning the value of "do {}" - possible perl bug?
by kyle (Abbot) on Nov 02, 2007 at 17:13 UTC

    I notice that bar will return 1 if you introduce a scalar context explicitly.

    sub bar{ if (1){ return scalar do { #do block will return 1 if (1){ 1; } 1; }; } }

    I notice also that B::Deparse turns 'if(1)' into do, so you also get '1' if you give the optimizer more to chew on:

    sub bar{ my $truth = 'beauty'; if ($truth){ return do { #do block will return 1 if ($truth) { 1; } 1; }; } }

    My guess is that the scalar context is getting lost in all those nested do blocks, but even so, I can't see why that would be a problem. It looks like a bug to me.

Re: Strange behaviour when returning the value of "do {}" - possible perl bug?
by gamache (Friar) on Nov 02, 2007 at 17:48 UTC
    Some simple test cases. Perl v5.8.8 on Linux and WinXP acted the same.
    my @testsubs; ## test 1 push @testsubs, sub { return do { 1; 1; } }; ## test 2 push @testsubs, sub { if (1) { return do { 1; } } }; ## test 3 push @testsubs, sub { if (1) { return do { 1; 1; } } }; ## test 4 push @testsubs, sub { if (1) { return scalar do { 1; 1; } } }; for (1..@testsubs) { print "test $_ ", $testsubs[$_-1]->()==1 ? 'passes' : 'fails', "\n"; }
    Output:
    test 1 passes test 2 passes test 3 fails test 4 passes
Re: Strange behaviour when returning the value of "do {}" - possible perl bug?
by samtregar (Abbot) on Nov 02, 2007 at 17:06 UTC
    Sure looks like a bug to me. I'd try it with the latest 5.9.x dev code - if it's still broken there then report it to perl5-porters. You can find details on how to do this in perlhack and perlbug.

    -sam

Re: Strange behaviour when returning the value of "do {}" - possible perl bug?
by moritz (Cardinal) on Nov 02, 2007 at 17:20 UTC
    I just tested it with blead (revision 32212) on Debian GNU/Linux 4.0 "Etch", it produces the same result.
Re: Strange behaviour when returning the value of "do {}" - possible perl bug?
by Prof Vince (Friar) on Nov 02, 2007 at 22:48 UTC
    Crossposted from p5p :

    This reduces to :
    use strict; use warnings; sub foo { if (1) { return do { 1; } } } sub bar { if (1) { return do { my $x; 1; } } } sub baz { if (1) { return scalar do { my $x; 1; } } } my $x = foo; my $y = bar; my $z = baz; print "#$x#\n"; print "#$y#\n"; print "#$z#\n";
    This looks like a duplicate of "my" old bug #38809. If you turn warnings on, you'll notice that bar returns undef. This seems to have to do with the return context, since changing to return scalar do fixes the problem.

    Edit: When ran into taint mode, foo fails too.

    Edit^2: Bah, exactly what kyle said !
Re: Strange behaviour when returning the value of "do {}" - possible perl bug?
by RenalPete (Acolyte) on Nov 02, 2007 at 18:48 UTC
    Thank you all, I have perlbuged it
Re: Strange behaviour when returning the value of "do {}" - possible perl bug?
by ambrus (Abbot) on Nov 03, 2007 at 16:57 UTC