`command 2>&1 > file` without the shell?

`command 2>&1 > file` without the shell?

am 15.09.2007 17:10:31 von kj

If I wanted to execute an external (unix) command X such that X's
normal output went to some file, but any error messages from X were
saved to an array, I could, for example, do this:

my @err = `X 2>&1 > some_file`;

How could I achieve the same results without using the shell?

TIA!

kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

Re: `command 2>&1 > file` without the shell?

am 15.09.2007 17:17:19 von Peter Makholm

kj writes:

> If I wanted to execute an external (unix) command X such that X's
> normal output went to some file, but any error messages from X were
> saved to an array, I could, for example, do this:
>
> my @err = `X 2>&1 > some_file`;
>
> How could I achieve the same results without using the shell?

You could use IPC::Open3 and then save the error messages youself.

//Makholm

Re: `command 2>&1 > file` without the shell?

am 15.09.2007 17:20:31 von Michal Nazarewicz

kj writes:

> If I wanted to execute an external (unix) command X such that X's
> normal output went to some file, but any error messages from X were
> saved to an array, I could, for example, do this:
>
> my @err = `X 2>&1 > some_file`;
>
> How could I achieve the same results without using the shell?

create a pipe, fork, play with opened files and execute X. Not tested
and lacking error checking:

#v+
pipe RD, WR;
if ($pid = fork) {
close WR;
@err = ;
waitpid $pid, 0;
close RD;
} else {
close RD;
open STDOUT, '>', 'some_file';
open STDERR, '>&', \*WR;
exec 'X' 'X';
}
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-------ooO--(_)--Ooo--

Re: `command 2>&1 > file` without the shell?

am 16.09.2007 12:23:41 von Ben Morrow

Quoth kj :
>
> If I wanted to execute an external (unix) command X such that X's
> normal output went to some file, but any error messages from X were
> saved to an array, I could, for example, do this:
>
> my @err = `X 2>&1 > some_file`;
>
> How could I achieve the same results without using the shell?

Use IPC::Run.

Ben