Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

robin (1821)

robin
  (email not shown publicly)
about:blank

Journal of robin (1821)

Wednesday August 29, 2001
07:33 AM

Want 0.05

[ #718 ]

In Perl 5.6.1 and later, there are some quite strict compile-time checks on the return value from an lvalue subroutine. Because the checks are done at compile-time, it doesn't matter if you know at run-time that you're going to be in lvalue context.

So code like:

sub foo :lvalue {
  if (want('RVALUE')) {
    return 23;
  } else {
    $foo;
  }
}

won't even compile. The new version of Want adds a function rreturn which you can use for an rvalue return from an lvalue sub. So the above can be written as:

sub foo :lvalue {
  if (want('RVALUE')) {
    rreturn 23;
  } else {
    $foo;
  }
  return;
}

The compiler sadly doesn't realise that rreturn will actually return from the function, so you have to put the bare return; at the end to shut it up. But you can think of that like the 1; that you have to put at the end of a module: meaningless furniture that has to be there.

Actually, the check is obviously too strict, because something like:

sub foo :lvalue {
  if (something_bad()) {
    die "Something bad happened";
  } else {
    $foo;
  }
}

ought to be okay. But in Perl 5.6.1 it won't compile. Can't modify die in lvalue subroutine return. I'm not trying to modify it, you brain-damaged compiler!