5.10 Lvalue Subroutines

     

Lvalue subroutines pretend to be assignable values, just like any ordinary variable. They do this by returning a proxy variable which handles the lvalue behavior for the subroutine (fetch, store, etc.). You declare an lvalue subroutine with the is rw property:

 sub storage is rw { . . . } storage( ) = 5; 

An lvalue sub can return an ordinary variable which acts as a proxy, return the return value from another lvalue sub, or it can return a tied proxy variable defined within the sub:

 my sub assignable is rw {     my $proxy is Proxy(         FETCH => { . . . },         STORE => { . . . },          . . .      );     return $proxy; } 

This example defines an lvalue sub named assignable . It creates a proxy variable tied to a Proxy class that defines FETCH and STORE tie methods on the fly.



Perl 6 and Parrot Essentials
Perl 6 and Parrot Essentials, Second Edition
ISBN: 059600737X
EAN: 2147483647
Year: 2003
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net