To correct that, you can just change the two "elsif ($next eq" lines to be:
elsif ($next and $next eq ...
That will check to make sure $next is defined before trying to compare it.
As to initializing a variable in perl, that just means assigning a value to it. The warning is just saying that $next is undefined (either because it hasn't been assigned to yet or because it was explicitly undef'd).