my $trace_level = 1; my $trace_file = 'tracefile.txt'; DBI->trace($trace_level, $trace_file); # ... then the rest of your script