My comment is not so much on how to do what you want since the above posts give very good suggestions, but only on your coding style. I would suggest adding the lines
use strict;
use warnings;
as well as I would look into using
Getopt::Long for getting your command line input.
cheers,
mav