Re: proposed addition to perlvar

[email protected] ("Chas. Owens")
Newsgroups perl.perl5.porters
Message-ID <[email protected]>
On Tue, Jun 8, 2010 at 16:29, Abigail <[email protected]> wrote:
> 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
>

How about:

=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 and using it
now triggers a severe warning: C<$# is no longer supported>.

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>.


If we are going to add $# back, should we add back $* as well?

=item $*

C<$*> used to be a variable that enabled multiline matching.
After a deprecated cycle, its magic was removed in 5.10.
Using it now triggers a severe warning: C<$* is no longer
supported>.  Use the C</s> and C</m> regexp modifiers
instead.

Also see L<perlre>.

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.