use strict;
use warnings;
use Cwd;
use 5.010;
# run command and print output to stdout, file, both or none.
# $print_to_stdout is boolean, $output_filename is optional
sub run_command {
my ( $command_string, $print_to_stdout, $output_filename ) = @_;
say "### run_command (from ", getcwd(), " ): ", $command_string, " ###";
# create output file if needed
my $output_fh;
if ( defined $output_filename ) {
open $output_fh, '>', $output_filename
or die "can't open $output_filename: $!";
print $output_fh "### run_command: ", $command_string, " ###\n";
}
# execute command
open( my $command_out, "-|", $command_string );
if ( $print_to_stdout || defined $output_fh ) {
while (<$command_out>) {
print if ($print_to_stdout);
print $output_fh $_ if ( defined $output_fh );
}
}
close $command_out; # wait until command has finsihed
close $output_fh if ( defined $output_fh );
}
####
my $command = "bp_genbank2gff3.pl -y -o out_dir some_genbank_filename";
####
run_command( $command, 1, undef );
run_command( $command, 0, "some_file_name");
run_command( $command, 1, "some_file_name");
####
run_command( $command, 0, undef );