diff options
author | Johan Vromans <jvromans@squirrel.nl> | 2013-06-21 17:31:54 +0200 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2013-06-21 12:38:52 -0700 |
commit | 771cc755419212f6b1b78673650a3a2e499c2908 (patch) | |
tree | 4e9385e97462797fc701aafdf708a9988334d576 /pod/perlsub.pod | |
parent | 9c1129c7de15ff8044f606550980c47f8c1724e9 (diff) | |
download | perl-771cc755419212f6b1b78673650a3a2e499c2908.tar.gz |
Update perlsub.pod for lvalue subroutines.
Diffstat (limited to 'pod/perlsub.pod')
-rw-r--r-- | pod/perlsub.pod | 36 |
1 files changed, 6 insertions, 30 deletions
diff --git a/pod/perlsub.pod b/pod/perlsub.pod index 027d7bea36..8a6ea6e0c8 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -764,9 +764,6 @@ also accepted. =head2 Lvalue subroutines X<lvalue> X<subroutine, lvalue> -B<WARNING>: Lvalue subroutines are still experimental and the -implementation may change in future versions of Perl. - It is possible to return a modifiable value from a subroutine. To do this, you have to declare the subroutine to return an lvalue. @@ -797,33 +794,12 @@ and in: all the subroutines are called in a list context. -=over 4 - -=item Lvalue subroutines are EXPERIMENTAL - -They appear to be convenient, but there is at least one reason to be -circumspect. - -They violate encapsulation. A normal mutator can check the supplied -argument before setting the attribute it is protecting, an lvalue -subroutine never gets that chance. Consider; - - my $some_array_ref = []; # protected by mutators ?? - - sub set_arr { # normal mutator - my $val = shift; - die("expected array, you supplied ", ref $val) - unless ref $val eq 'ARRAY'; - $some_array_ref = $val; - } - sub set_arr_lv : lvalue { # lvalue mutator - $some_array_ref; - } - - # set_arr_lv cannot stop this ! - set_arr_lv() = { a => 1 }; - -=back +Lvalue subroutines are convenient, but you have to keep in mind that, +when used with objects, they may violate encapsulation. A normal +mutator can check the supplied argument before setting the attribute +it is protecting, an lvalue subroutine cannot. If you require any +special processing when storing and retrieving the values, consider +using the CPAN module Sentinel or something similar. =head2 Lexical Subroutines X<my sub> X<state sub> X<our sub> X<subroutine, lexical> |