views:

227

answers:

2

I can't seem to find explicit documentation for $# in perldoc.

Yes, I know what it is, and I can see implicit references in places like perllol and perldsc, but I can't seem to find anything standalone.

What's really surprising is that it doesn't have it's own section in perlvar.

Does such documentation exist? If so, where?

+14  A: 

$# is not a special variable, it is a sigil like $, @, %, &, and *. Since it is just part of how arrays work, you will find it documented in perldoc perldata with the rest of the documentation on how variables work.

$#foo is just a way of asking @foo what its last index is. There is no separate symbol table entry for it (like there is for $foo, %foo, &foo, and the filehandle foo).

Chas. Owens
Hmm, I'd have thought it would be 'special'. Oh, does `$#` have a name? 'Last-array-element-subscript' is the best I can come up with atm.
Zaid
@Zaid I normally pronounce `$#foo` as "last index of foo".
Chas. Owens
`$#` maybe should be in perlvar. It isn't a variable, but imagine a newbie who wants to understand what `$#blah` does. Where is s/he going to look? Knowing to look in perldata requires knowing that `$#` is related to Perl data types. How do you find the answer if you don't know the answer? Search? Searching for things like `$#` and `$/` doesn't work well as most search systems strip nonword characters from their indexes. Perl's docs are extensive and well written, but they are hard for a newbie to use. I don't have a good suggestion for how to fix this, but I want to call attention to it.
daotoad
@daotoad : Agreed.
Zaid
@daotoad, so you think we should add `=item $#\n\nC<$#> is not actually a variable. It is a sigil that can be prepended to an array's name to get the last index in that array (e.g. $#foo is the last index in @foo). For more information see L<perldata>.\n\n` to `perlvar.pod`?
Chas. Owens
A patch has been suggest to p5p: http://www.nntp.perl.org/group/perl.perl5.porters/2010/06/msg160812.html
Chas. Owens
+5  A: 

Actually, $# used to be a builtin variable:

$#

The output format for printed numbers. This variable is a half-hearted attempt to emulate awk's OFMT variable. There are times, however, when awk and perl have differing notions of what is in fact numeric. Also, the initial value is %.20g rather than %.6g, so you need to set $# explicitly to get awk's value. (Mnemonic: # is the number sign.)

$# was removed in 5.10.0.

See:

C:\> perl -e "print $#"
$# is no longer supported at -e line 1.

See also How do you google for -f>@+?*<.-&'_:$#/%!

Sinan Ünür
I thought I remembered `$#` having a life of its own, but it wasn't there when I went search the POD. Looks like it got removed in Perl 5.10.0.
Chas. Owens
@Chas. According to http://search.cpan.org/~andyd/perl5.003_07/pod/perlvar.pod `$#` was deprecated in Perl 5. It was removed with `5.10.1`. See http://search.cpan.org/~dapm/perl-5.10.1/pod/perl5100delta.pod#The_%24%2a_and_%24%23_variables_have_been_removed
Sinan Ünür
Interesting, it was removed from `perlvar` in Perl 5.10.0 (http://perldoc.perl.org/5.10.0/perlvar.html) it used to come right before `$FORMAT_PAGE_NUMBER`.
Chas. Owens
It looks like they just reiterated the removal in Perl 5.10.1, because it is listed in Perl 5.10.0's perldelta: http://search.cpan.org/~rgarcia/perl-5.10.0/pod/perl5100delta.pod#The_$*_and_$#_variables_have_been_removed
Chas. Owens
Interesting history behind `$#`... seems that my gut instinct to check `perlvar` was not completely unfounded after all.
Zaid
@Chas. Thanks for the correction. I thought it might be a good idea to re-insert an entry for `$#` in `perlvar.pod` pointing the user to `perldata.pod` and sent a patch proposal to `perl5-porters`. Hope I haven't made a mistake.
Sinan Ünür
@Chas. It turns out *I did make a mistake*. I did check but apparently I missed your message to `perl5-porters` with the same idea. Oooops!
Sinan Ünür