PLEASE Don't Use lvalue!

Ovid on 2008-01-21T18:12:09

Run this code:

#!/usr/bin/env perl

use strict;
use warnings;

my $val;
sub val : lvalue { $val }

val = 3;
print $val;

That prints three.

Now run it through the debugger:

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(test.pl:6):      my $val;

  DB<1> c
Use of uninitialized value in print at test.pl line 10.
 at test.pl line 10

lvalue code breaks in the debugger. Not only can you not validate lvalues (unless you jump through hoops with tieing the variable), you can't run the debugger on it. The tiny, tiny amount of syntactic sugar isn't worth this, is it? Of course, let's not forget that accidentally adding a return statement in an lvalue sub breaks it without warning. You just have to know that since you can't debug it.


Broken just temporarily, right?

jdavidb on 2008-01-21T19:39:20

Seems like the solution is to declare that this mechanism is broken for now, and fix it so that it works with the debugger, as well as fix the compiler so that it generates a warning or error when return is called from an lvalued subroutine.

The tiny little amount of syntactic sugar may indeed be worth it for some people, particularly if the option to fix the bug you're reporting is open.

broke and patch submitted

deepfryed on 2008-01-22T01:04:15

yes it is broken and I have submitted a patch, probably someone will look into it soon

http://rt.perl.org/rt3/Ticket/Display.html?id=48489

Re:broke and patch submitted

Ovid on 2008-01-22T07:37:58

That's great news. I noted your editor trims whitespace at the end of lines and that makes it rather hard to read the patch, but I'm still quite happy to see this :)