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)

Saturday August 04, 2001
07:51 AM

lvalue subroutines made easy

[ #605 ]

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.