Good programming style: High Cohesion, low coupling. That doesn't only apply across modules, but also applies to functions and code within the same module.
I will use a small sub to return the count so that how you actually implement the count is abstracted from the caller's logic:
for $i (0..varcount($some_variable)) {
...
}
sub varcount
{
# you can implement the count any way you like
# and change the implementation any time you like,
# the caller will not get affected, as long as the
# interface to varcount is unchanged
}