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

Is it possible to create lvalue subs which are defined at run-time? For example, a non-lvalue sub which returns the value of some local variable can be defined at run-time by:
*mysub = eval 'sub { $myvar; }'
I would like to do something like:
*mysub = eval 'sub : lvalue { $myvar; }'
The second code snippet is not very well liked by Perl. In case you're wondering why anyone not clinically insane would ever want to do such a thing, the use is for a module which generates and infuses "boilerplate" code for Perl classes. Any ideas on how to make it so?

Replies are listed 'Best First'.
Re: lvalue subs of a run-time persuasion
by mdillon (Priest) on Dec 06, 2000 at 09:02 UTC
    under 5.6.0, this works:
    my $mysub = do { my $myvar; eval 'return sub : lvalue { $myvar }'; }; $mysub->() = 42; print $mysub->(), $/;
    Outputs:
    42
    
    as i've heard many times before, the new features in 5.6.0 are a buggy area at times. this seems to be an inconsistency that should be checked, or submitted as a bug.
      Thanks, this did the trick. "Experimental" indeed...
Re: lvalue subs of a run-time persuasion
by extremely (Priest) on Dec 06, 2000 at 08:51 UTC
    I believe that lvalue-able subs are on the wish list but I don't think they are available today. And I'm pretty darn certain they wouldn't use a syntax like that.

    update: open mouth, insert foot. Pretty sleek stuff, I gotta start reading the perldocs from 5.6 rather than the elder versions...

    --
    $you = new YOU;
    honk() if $you->love(perl)

      Perl 5.6 already has them (although it's labeled "experimental"), and they do use the above syntax (minus the eval stuff, which is needed to define and compile sub code at run-time). More specifically:
      sub mysub : lvalue { $myvar; }
      Check out perlsub for a more thorough discussion.
        I didn't know they had turned em on without a compile time flag. Cool. The syntax still sucks. I thought there was a raging argument on that...

        --
        $you = new YOU;
        honk() if $you->love(perl)