ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
14. Process ManagementContents: 14.1 Using system and execLike the command shell, a Perl program can launch new processes, and like most other operations, has more than one way to do so. The simplest way to launch a new process is to use the system("dir"); We're ignoring the return value here, but the dir command is not likely to fail anyway. Where does the command's output go? In fact, where does the input come from, if it was a command that wanted input? These are good questions, and the answers to these questions are most of what distinguishes the various forms of process creation. For the system("dir >this_dir") && die "cannot create this_dir"; This time, we not only send the output of the dir command into a file with a redirection to the new command shell, but also check the return status. If the return status is true (nonzero), something went wrong with the shell command, and the Here's an example of generating a dir command and sending the output to a filename specified by a Perl variable: $where = "dir_out.".++$i; # get a new filename system "dir >$where"; The double-quoted string is variable interpolated, so Perl replaces In addition to the standard filehandles, the current directory and the environment variables are inherited by the child. These variables are typically created by the command shell set command and accessed or altered using the %KEYNAME% construct. Environment variables are used by many utilities, including the command shell itself, to alter or control the way that utility operates. Perl gives you a way to examine and alter current environment variables through a special hash called For example, here's a simple program that prints out all of your environment variables: foreach $key (sort keys %ENV) { print "$key=$ENV{$key}\n"; } Note that the equal sign here is not an assigment, but simply a text character that the Here's a program snippet that alters the value of $oldPATH = $ENV{"PATH"}; # save previous path $ENV{"PATH"} = "c:\\msdev\\bin;c:\\winnt;c:\\winnt\\system32"; # force known path system("nmake myproj.mak >output"); # run command $ENV{"PATH"} = $oldPATH; # restore previous path That's a lot of typing. It'd be faster just to set a local value for this hash element. Despite its other shortcomings, the { local $ENV{"PATH"} = "c:\\msdev\\bin;c:\\winnt;c:\\winnt\\system32"; system("nmake fred bedrock >output"); } The system "nmake 'fred flintstone.mak' buffaloes"; # using command shell system "nmake","fred flintstone.mak","buffaloes"; # using list Giving Here's another example of equivalent forms: @cfiles = ("fred.c","barney.c"); # what to compile @options = ("-DHARD","-DGRANITE"); # options system "cc -o slate @options @cfiles"; # using shell system "cc","-o","slate",@options,@cfiles; # avoiding shell |