Mandor has asked for the wisdom of the Perl Monks concerning the following question:

I`ve seen this variable in some modules and in a code
example in perltoot. As I know that one should RTFM
I checked perlvar but didn't find any entry for this
rather distinguished-looking fellow.

So I'd like to ask you monks what is the purpose of this
variable is.

Replies are listed 'Best First'.
Re: What is $#_ ?
by suaveant (Parson) on Nov 16, 2001 at 02:49 UTC
    $# is the last array index of an array @... so $#_ is the last index of the array @_ , which holds the arguments of a subroutine. If @_ holds three items... $#_ == 2

                    - Ant
                    - Some of my best work - (1 2 3)

Re: What is $#_ ?
by Beatnik (Parson) on Nov 16, 2001 at 02:51 UTC
    Since $#Array returns the index of the last element of @Array, $#_ returns the index of the last element of @_. @_ contains the arguments made to a subroutine. See perlvar for more info.

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: What is $#_ ?
by rob_au (Abbot) on Nov 16, 2001 at 02:51 UTC
    This variable returns the number of array elements in @_ - For example:

    #!/usr/bin/perl -Tw use strict; my @array = (1..5); # Pass @array to the &count subroutine and print returned result # print "There are ", &count(@array), " elements in \@array.\n"; sub count { # The arguments are passed to &count and are in @_ # return $#_ + 1; };

     

    Ooohhh, Rob no beer function well without!

      This variable returns the number of array elements in @_

      <pedant>
      Not exactly. $#_ contains the index of the last element in @_. This is actually one less than the number of elements in @_ (assuming that you haven't messed with the value of $[ - which you should never do[1]).
      </pedant>

      [1] In fact, please forget[2] I ever mentioned that you can.
      [2] Please look into the red light *FLASH*

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."