« lack of -r flag in grep | Main | Foyle Fishing Permits & Licences »

is and are

I'm adding a new category called snippets, for all those little things I often find myself looking for over and over again.
I must admit I am often lazy when it comes to making sure my grammar is correct if there is only 1 item in an array or a hash.
So in order to ensure that my text reads,
there "is" one item in bar or
there "are" items in bar.
my $foo = ( scalar @$bar > 1 ) ? "are $num items" : "is $num item";

Comments

Try this instead :

my $foo = ( scalar @$bar == 1 ) ? "is $num item" : "are $num items";

This will ensure it's correct for zero items too.

Post a comment

The following bizarre question is in place to try to stop Comment Spam.