in reply to perl doesn't like variable

You can also use "our":
our $counter = 0;