my $foo = [ 'a', ['b', 'c', [ 'd' ]] ]; print counter($foo); sub counter { my $foo = shift; my $count = 0; foreach (@$foo) { if (ref($_) eq 'ARRAY') { $count += counter($_); } else { $count++; } } return $count; }