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 ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday January 21, 2008
01:12 PM

PLEASE Don't Use lvalue!

[ #35443 ]

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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.

    --
    J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
  • 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 [perl.org]
    • 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 :)