From: M.J.T. Guy Date: Mon, 24 Mar 1997 07:25:21 +0000 (+0000) Subject: pods for subroutine argument autovivication X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=db8878faa51a8a1541a40745a8613adb5db155e4;p=p5sagit%2Fp5-mst-13.2.git pods for subroutine argument autovivication Here are pod updates for subroutine argument autovivication. I'll also try to do some additions to the tests. One small detail noted: perl -we 'sub f { print @_ }; f $a[0]' produces Use of uninitialized value at -e line 1. under Perl 5.003 but is silent under Perl5.003_94. Do we care? p5p-msgid: E0w9489-0005YT-00@ursa.cus.cam.ac.uk --- diff --git a/pod/perldelta.pod b/pod/perldelta.pod index 958bee3..1e32990 100644 --- a/pod/perldelta.pod +++ b/pod/perldelta.pod @@ -27,17 +27,23 @@ might have symbol conflicts if you embed Perl in another application, just as in the 5.003 release. By default, binary compatibility is preserved at the expense of symbol table pollution. -=head2 No Autovivification of Subroutine Parameters - -In Perl versions 5.002 and 5.003, array and hash elements used as -subroutine parameters were "autovivified"; that is, they were brought -into existence if they did not already exist. For example, calling -C would create C<$h{foo}> if it did not already exist, -causing C to become true and C to return -C<('foo')>. - -Perl 5.004 returns to the pre-5.002 behavior of I autovivifying -array and hash elements used as subroutine parameters. +=head2 Creation of Subroutine argumnets only when necessary + +In Perl 5.004, array and hash elements used as subroutine parameters +are brought into existence if they did not already exist only if the +argument is actually assigned to. In Perl versions 5.002 and 5.003, +such arguments were always brought into existence. Versions before +5.002 never brought such arguments into existence. +For example, after + + undef @a; undef %a; + sub foo { print $_[0] }; + sub bar { $_[0]++ }; + foo($a[2]); + bar($a{b}); + +$a{b} exists but $a[2] does not in Perl5.004; in Perl 5.002 and 5.003 +both would exist. =head2 Fixed Parsing of $$, &$, etc. diff --git a/pod/perlsub.pod b/pod/perlsub.pod index f2a5b8f..2710774 100644 --- a/pod/perlsub.pod +++ b/pod/perlsub.pod @@ -47,9 +47,17 @@ 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) to the actual scalar parameters. The -return value of the subroutine is the value of the last expression +and C<$_[1]>. The array @_ is a local array, but its elements are +aliases for the actual scalar parameters. In particular, if an element +C<$_[0]> is updated, the corresponding argument is updated (or an error +occurs if it is not updatable). If an argument is an array or +hash element which does not exist, the element is created if it is +assigned to or if a reference to it is taken. (Some earlier versions +of Perl created the element whether or not it was assigned to.) +Note that assigning to the whole array @_ removes the aliasing, +and does not update any arguments. + +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