Re: proposed addition to perlvar
[email protected] (Abigail)
| Newsgroups | perl.perl5.porters |
|---|---|
| Message-ID | <20100608202931.GB22278@almanda> |
On Tue, Jun 08, 2010 at 12:22:38PM -0400, Chas. Owens wrote:
> Due to some [confusion a new user had][1] finding the documentation
> for what $# does in $#foo, I suggest we add a small note to perlvar to
> point people in the right direction (patch inlined).
>
> diff --git a/pod/perlvar.pod b/pod/perlvar.pod
> index ed90610..359dd62 100644
> --- a/pod/perlvar.pod
> +++ b/pod/perlvar.pod
> @@ -879,6 +879,17 @@ as described below.
>
> Also see L<Error Indicators>.
>
> +=item $#
> +
> +C<$#> is not a variable; it is a sigil like C<@> that can be prepended
> +on the name of an array to get the index of the last item in that array.
> +
> + my @a = ("a", "b", "c");
> + my $last_index = $#a; # $last_index is 2
> + my $last_element = $a[$#a]; # $last_element is "c"
> +
> +Also see L<perldata>.
> +
But $# *is* a variable. Until recently (5.8.9), it was even a variable
with magic, and hence documented in perlvar. Nowadays, it's a somewhat
normal variable:
$ perl -wE '$# = 3; say $#'
$# is no longer supported at -e line 1.
3
$
Perhaps it's best to re-add $# to perlvar, and write something like:
=item $#
C<$#> used to be a variable that could be used to format printed number.
After a deprecation cycle, its magic was removed in 5.10.
C<$#> is also used as sigil, which, when prepended on the name of an
array gives the index of the last element in that array.
my @a = ("a", "b", "c");
my $last_index = $#a; # $last_index is 2
my $last_element = $a[$#a]; # $last_element is "c"
Also see L<perldata>.
I had not realized the $# entry was complete removed from perlvar. I don't
think it's a bad thing to have a little perl history in the docs. And since
its use does trigger a warning, it *is* in a way a special variable. And hence
belongs in perlvar.
Abigail