The problem is that $opt_i here is lexically scoped. Try use vars qw($opt_i); instead of my $opt_i;.
In reply to Re: Using getopt by FoxtrotUniform in thread Using getopt by idunno