diff options
-rw-r--r-- | pod/perlsub.pod | 11 | ||||
-rw-r--r-- | pod/perltrap.pod | 20 |
2 files changed, 29 insertions, 2 deletions
diff --git a/pod/perlsub.pod b/pod/perlsub.pod index 347d2f8c92..a38d05be25 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -48,8 +48,15 @@ there's really no difference from the language's perspective.) Any arguments passed to the routine come in as the array @_. Thus if you called a function with two arguments, those would be stored in C<$_[0]> and C<$_[1]>. The array @_ is a local array, but its values are implicit -references (predating L<perlref>) to the actual scalar parameters. The -return value of the subroutine is the value of the last expression +references (predating L<perlref>) to the actual scalar parameters. What +this means in practice is that when you explicitly modify C<$_[0]> et al., +you will be changing the actual arguments. As a result, all arguments +to functions are treated as lvalues. Any hash or array elements that are +passed to functions will get created if they do not exist (irrespective +of whether the function does modify the contents of C<@_>). This is +frequently a source of surprise. See L<perltrap> for an example. + +The return value of the subroutine is the value of the last expression evaluated. Alternatively, a return statement may be used to specify the returned value and exit the subroutine. If you return one or more arrays and/or hashes, these will be flattened together into one large diff --git a/pod/perltrap.pod b/pod/perltrap.pod index 05cb02b47b..a179b8b5a8 100644 --- a/pod/perltrap.pod +++ b/pod/perltrap.pod @@ -1137,6 +1137,26 @@ general subroutine traps. Includes some OS-Specific traps. =over 5 +=item * Subroutine calls provide lvalue context to arguments + +Beginning with version 5.002, all subroutine arguments are consistently +given a "value may be modified" context, since all subroutines are able +to modify their arguments by explicitly referring to C<$_[0]> etc. +This means that any array and hash elements provided as arguments +will B<always be created> if they did not exist at the time +the subroutine is called. (perl5 versions before 5.002 used to provide +lvalue context for the second and subsequent arguments, and perl4 did +not provide lvalue context to subroutine arguments at all--even though +arguments were supposedly modifiable in perl4). + + sub test { $_[0] = 1; $_[1] = 2; $_[2] = 3; } + &test($foo{'bar'}, $bar{'foo'}, $foo[5]); + print join(':', %foo), '|', join(':',%bar), '|', join(':',@foo); + + # perl4 prints: || + # perl5 < 5.002 prints: |foo:2|:::::3 + # perl5 >= 5.002 prints: bar:1|foo:2|:::::3 + =item * (Signals) Barewords that used to look like strings to Perl will now look like subroutine |