The shorthand to do so in bash or another console that supports it is: $ perl mycode.pl &> debug.txt
or the more explicit: $ perl mycode.pl 2>1 1> debug.txt
Both of these redirect streams STDERR and STDOUT to a file.
Also see: open 'STDERR', '>', './File' or die $!; and: open 'STDERR', '>', './File' or die $!;
Which would make it so the streams are rediected inside of the program rather than by the console.