Perl solution for problem 6.