Perl - how to get the number of elements in a list (not a named array)
- by NXT
Hi Everyone,
I'm trying to get a block of code down to one line. I need a way to get the number of items in a list. My code currently looks like this:
# Include the lib directory several levels up from this directory
my @ary = split('/', $Bin);
my @ary = @ary[0 .. $#ary-4];
my $res = join '/',@ary;
lib->import($res.'/lib');
That's great but I'd like to make that one line, something like this:
lib->import( join('/', ((split('/', $Bin)) [0 .. $#ary-4])) );
But of course the syntax $#ary is meaningless in the above line.
Is there equivalent way to get the number of elements in an anonymous list?
Thanks!
PS: The reason for consolidating this is that it will be in the header of a
bunch of perl scripts that are ancillary to the main application, and I want this
little incantation to be more cut & paste proof.