lvalue subroutines made easy

robin on 2001-08-04T12:51:05

I've just uploaded Want 0.04 to CPAN. The big news is that I've added what I call ASSIGN context.

It means that you can write clever lvalue subroutines without having to do a lot of tie magic. Here's an example:

sub backstr :lvalue {
    if (want(qw'LVALUE ASSIGN')) {
        my ($a) = want('ASSIGN');
        $_[0] = reverse $a;
         return undef;
    }
    elsif (want('RVALUE')) {
        my $t = scalar reverse $_[0];
    }
    else {
         carp("Not in ASSIGN context");
    }
}

print "foo -> ", backstr("foo"), "\n"; # foo -> oof
backstr(my $robin) = "nibor";
print "\$robin is now $robin\n";       # $robin is now robin

That's like the reverse function, except that you can assign to it.

I've just come out of Philippe Bruhat's amazing talk, in which he explained some obfuscated programs (including Erudil's camel code, and Philippe's own bilingual Perl/Postscript program). It's delightful to meet such people.