Perl solution of problem 2.