diff options
author | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2004-02-03 21:54:23 +0000 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2004-02-03 21:54:23 +0000 |
commit | a4fb829857308f846c16fee81060adc844317666 (patch) | |
tree | a16b1b79fa4df04e7e63b63e4e97543018302e5c /pod | |
parent | 137d6fc09ef3595c225f4474cf527a89e2099776 (diff) | |
download | perl-a4fb829857308f846c16fee81060adc844317666.tar.gz |
More docs and tests for "my $_".
p4raw-id: //depot/perl@22265
Diffstat (limited to 'pod')
-rw-r--r-- | pod/perlfunc.pod | 10 | ||||
-rw-r--r-- | pod/perlsub.pod | 2 |
2 files changed, 12 insertions, 0 deletions
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index a7bbacc9fc..13cfdab3c8 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -2230,6 +2230,11 @@ element of a list returned by grep (for example, in a C<foreach>, C<map> or another C<grep>) actually modifies the element in the original list. This is usually something to be avoided when writing clear code. +If C<$_> is lexical in the scope where the C<grep> appears (because it has +been declared with C<my $_>) then, in addition the be locally aliased to +the list elements, C<$_> keeps being lexical inside the block; i.e. it +can't be seen from the outside, avoiding any potential side-effects. + See also L</map> for a list composed of the results of the BLOCK or EXPR. =item hex EXPR @@ -2615,6 +2620,11 @@ Using a regular C<foreach> loop for this purpose would be clearer in most cases. See also L</grep> for an array composed of those items of the original list for which the BLOCK or EXPR evaluates to true. +If C<$_> is lexical in the scope where the C<map> appears (because it has +been declared with C<my $_>) then, in addition the be locally aliased to +the list elements, C<$_> keeps being lexical inside the block; i.e. it +can't be seen from the outside, avoiding any potential side-effects. + C<{> starts both hash references and blocks, so C<map { ...> could be either the start of map BLOCK LIST or map EXPR, LIST. Because perl doesn't look ahead for the closing C<}> it has to take a guess at which its dealing with diff --git a/pod/perlsub.pod b/pod/perlsub.pod index 969d0ba039..e830130a55 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -588,6 +588,8 @@ separator. Notably, if you want to work with a brand new value of the default scalar $_, and avoid the potential problem listed above about $_ previously carrying a magic value, you should use C<local *_> instead of C<local $_>. +As of perl 5.9.1, you can also use the lexical form of C<$_> (declaring it +with C<my $_>), which avoids completely this problem. =head3 Localization of elements of composite types |