In general:
- use the
strict and warnings pragmas
- use 3 argument open with a lexical handle
- test to see if your open call succeeds before continuing your script
- if you are numbering your variable names (
$input1, $input2) use an array instead (@inputs)
- if you want to use a variable name as a variable, use a hash instead
- when in doubt, RTFM. Since TFM is big and hard to find things in at first, you might want to read How To RTFM
- The FAQs are pretty handy too. There's a whole section on system interaction, and another on file manipulation.
- when you need a function to do some random task, look at the functions by category section in perlfunc.
No matter what, you will need to define what you are trying to do a little more specifically.
Do you want to run your command and dump its STDOUT to a file:
`pkgrm $package > $outfile`;
Or maybe append the results to an existing log:
`pkgrm $package >> $outfile`;
Or do you want to catch the results and see what happened in your program:
open( my $result_fh, '>', $outfile ) or die "can't open logfile - $!\n";
my $output = `pkgrm $package`;
if( $oupt =~ /happiness/) {
print $result_fh "hooray!\n", $output;
}
else {
print $result_fh "uh oh\n", $output;
}
Or do you need to monitor standard error?
Or should STDERR and STDOUT be combined into one stream?
For these cases and more see perlfaq8.