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 }