It is pretty much a “given” that, no matter what common task it is that you’re doing, someone out there on CPAN has done it .. and has done it well. So, the first course of action in almost every case should be to first use “Super Search” here, then to go to http://search.cpan.org.
CPAN modules are routinely accompanied by a thorough self-test suite that runs automatically on your system as part of the installation sequence. (The module won’t install unless it first passes all of the tests, and there could be several hundred of these.) So, by choosing the highest-level module that matches your problem most completely, you come away with a thoroughly written and tested solution that you didn’t have to write.
Actum ne agas – do not do a thing already done.