Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.
Due to a question asked at work, I remembered that I see a lot of Perl books (PERL books, actually), recommend the following as a way of counting the elements in an array:
my $count = $#array + 1;
On the off chance you do this, it's wrong. $count is now assigned a value one greater than the last index value, not the number of elements in the array. Almost every single time you do this, you're probably going to get the answer you expect, but because you're using something for a purpose other than what it was intended for, you should not be surprised when it doesn't always do what you want.
$ perl -le '$[ = 4; my @a = qw<foo bar baz>; print $#a + 1'
Sure, you could argue that you will never alter the contents of $[, so you're therefore safe. The problem is, you know that sometimes that can give the wrong answer. Why use something you know won't always work? I don't get it. Just use context. It's both correct and easier to read.
my $count = @array;
# or, if you must be explict:
print "We have ", scalar @orders, " order(s)";
Perly Warts (Score:2)
The problem is that
$[was added for one specific purpose: to vary the base index between 0 and 1 as an aid to Fortran programmers who had trouble adjusting to the world of zero based indexing. Most people don't realize that$[exists, or why it exists, sadly.The issue here is that the idiom for counting elements in an array should be
$count = @array. However, the$count = $#array + 1idiom (mostly) works, and meets another criteria Perl aims to fRe:Perly Warts (Score:1)
If it (mostly) works then you should tell them how it always works. No?
It's not any worse than... (Score:2)
Re:It's not any worse than... (Score:2)
Re:It's not any worse than... (Score:2)
Worse yet, you should almost never use that index. Try to do
if at all possible. The only time I use an index is when I have two parallel arrays. Using this other idiom completely eliminates the index: you just access the things you want to get to directly.J. David works really hard, has a passion for writing good software, and knows many of the world's best Perl programmers
Re:It's not any worse than... (Score:2)
Ugh. My beautiful argument is lying there twitching. I'll shut up now.
Won't Always Work (Score:1)
Good luck with that argument. Meanwhile, my campaign to get people to call methods as methods, not functions, has stalled.
OTOH... (Score:1)
There is a small advantage of using $#array + 1 over @array: the former will give you the number of elements regardless of its context (assuming $[ isn't set). @array won't give you the number of elements when used in list context.
And I bet more people get bitten by an unexpected context, then by $[ being set to a non-zero value.
Nooo! Don't tell people to fix this! (Score:1)