Contributed by flymolo
on Jun 21, 2000 at 01:59 UTC
Q&A
> programs and processes
Description: `some command` will give me the text of the executed program.
system("command") will return the return value, but running the
program twice might change text and/or return values.
Is there any easy way to do this?
Right now I'm thinking system("command >file") but then I'd
have a file I don't need.
Answer: How do I get both the return value and text? backticks vs. system() contributed by chromatic Use backticks to get output from an executed program. Check $? for the error returned.
A pipe would work in this case, but backticks are probably simpler. | Answer: How do I get both the return value and text? backticks vs. system() contributed by lhoward open has a piping syntax that you can use to pipe
data to or from a program.
You can get the exit value from $? (see perlvar).
open P,"command |" or die "error running command $!";
my @data=<p>;
close P;
my $exit_value=$? >> 8;
| Answer: How do I get both the return value and text? backticks vs. system() contributed by Archon810 Having used the new Perl 5.10, I was shocked to find this new variable after months of use:
${^CHILD_ERROR_NATIVE}
This variable gives the native status returned by the last pipe close, backtick command, successful call to wait() or waitpid(), or from the system() operator. See perlrun for details. (Contributed by Gisle Aas.)
New internal variables
Finally!.. what else can I say? | Answer: How do I get both the return value and text? backticks vs. system() contributed by je44ery If you especially don't want the shell interpreting shell metacharacters, then don't pass the command to the shell, use the 4+ argument form of open.
open(CMD, '-|', '/usr/bin/ls', '$4', '$PATH');
my $output = do { local $/; <CMD> };
close CMD;
print "$output\n";
Here is the output, you'll see that the "$4" and "$PATH" were not interpreted as variables before being sent to ls.
ls: 0653-341 The file $4 does not exist.
ls: 0653-341 The file $PATH does not exist.
|
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|