`

Note of Learning Perl--Process Management

    博客分类:
  • Perl
阅读更多

Process Management
------------------------

1. The exec Function
 Because Perl is no longer in control once the requested command has started, it doesn't make any sense to have any Perl code following the exec, except for handling the error when the requested command cannot be started

2. The Environment Variables
 For example, suppose you wished to run the system's make utility (which typically runs other programs), and you want to use a private directory as the first place to look for commands (including make itself). And let's say that you don't want the IFS environment variable to be set when you run the command, because that might cause make or some subcommand do the wrong thing. Here we go:
 $ENV{'PATH'} = "/home/rootbeer/bin:$ENV{'PATH'}";
 delete $ENV{'IFS'};
 my $make_result = system "make";
 
3. Using Backquotes to Capture Output
 1) Standard error of a backquoted command is inherited from Perl's current standard error output. If the command spits out error messages to standard error, you'll probably see them on the terminal, which could be confusing to the user who hasn't personally invoked the frobnitz command. If you want to capture error messages with standard output, you can use the shell's normal "merge standard error to the current standard output," which is spelled 2>&1 in the normal Unix shell:
  my $output_with_errors = `frobnitz -enable 2>&1`; 
 2) So, stay away from commands that read standard input. If you're not sure whether something reads from standard input, then add a redirection from /dev/null for input, like this:
  my $result = `some_questionable_command arg arg argh </dev/null`;
  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics