You've been given the right answer to your problem, but I would suggest that you learn to indent your code correctly. Although wrong indentation does not prevent your code from running, right indentation is absolutely essential to programming correctly. An additional recommendation would be to use:
use strict;
use warnings;
for any program having more than one line. These two bits of advice will save you hours of debugging time if you follow them.
Please don't hesitate to ask if you don't understand what I am talking about or why I am saying such things.