X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlsub.pod;h=2969341ca12d183935d162b79f30ab5d482d8893;hb=b30f304ae36b3931349d7d5816f5a5646afe5397;hp=cff3edadfa3bc32838767b6285dfa1e6706795c8;hpb=17b63f683fb43fba07a1f7dfb8799f26fab23ec1;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlsub.pod b/pod/perlsub.pod index cff3eda..2969341 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -220,9 +220,9 @@ Synopsis: my @oof = @bar; # declare @oof lexical, and init it my $x : Foo = $y; # similar, with an attribute applied -B: The use of attribute lists on C declarations is -experimental. This feature should not be relied upon. It may -change or disappear in future releases of Perl. See L. +B: The use of attribute lists on C declarations is still +evolving. The current semantics and interface are subject to change. +See L and L. The C operator declares the listed variables to be lexically confined to the enclosing block, conditional (C), @@ -325,14 +325,8 @@ it. Similarly, in the conditional the scope of $answer extends from its declaration through the rest of that conditional, including any C and C clauses, -but not beyond it. - -B The behaviour of a C statement modified with a statement -modifier conditional or loop construct (e.g. C) is -B. The value of the C variable may be C, any -previously assigned value, or possibly anything else. Don't rely on -it. Future versions of perl might do something different from the -version of perl you try it out on. Here be dragons. +but not beyond it. See L for information +on the scope of variables in statements with modifiers. The C loop defaults to scoping its index variable dynamically in the manner of C. However, if the index variable is @@ -563,6 +557,13 @@ hash to some other implementation: } [..%ahash back to its initial tied self again..] +B The code example above does not currently work as described. +This will be fixed in a future release of Perl; in the meantime, avoid +code that relies on any particular behaviour of localising tied arrays +or hashes (localising individual elements is still okay). +See L for more +details. + As another example, a custom implementation of C<%ENV> might look like this: @@ -615,14 +616,15 @@ types is subject to change in future. =head2 Lvalue subroutines -B: Lvalue subroutines are still experimental and the implementation -may change in future versions of Perl. +B: 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. my $val; sub canmod : lvalue { + # return $val; this doesn't work, don't say "return" $val; } sub nomod { @@ -648,6 +650,39 @@ 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 are several reasons to be +circumspect. + +You can't use the return keyword, you must pass out the value before +falling out of subroutine scope. (see comment in example above). This +is usually not a problem, but it disallows an explicit return out of a +deeply nested loop, which is sometimes a nice way out. + +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 + =head2 Passing Symbol Table Entries (typeglobs) B: The mechanism described in this section was originally @@ -1116,8 +1151,8 @@ only occasionally and for good reason. Typically this might be done by a package attempting to emulate missing built-in functionality on a non-Unix system. -Overriding may be done only by importing the name from a -module--ordinary predeclaration isn't good enough. However, the +Overriding may be done only by importing the name from a module at +compile time--ordinary predeclaration isn't good enough. However, the C pragma lets you, in effect, predeclare subs via the import syntax, and these names may then override built-in ones: @@ -1319,7 +1354,7 @@ parsed and invoked: use attributes __PACKAGE__, \&plugh, q[Ugly('\(")], 'Bad'; For further details on attribute lists and their manipulation, -see L. +see L and L. =head1 SEE ALSO