in reply to Changing the order of printing

You seem to have been nearly there with your solution. Just create an array (!!) with 'my @outarray;', change any 'print OUT' to 'push @outarray,' and at the exit point of your script add 'print LOG @outarray;'.

Or use BrowserUk's suggestion, which works as well