Click here to get back home

Example for open3 on windows?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Example for open3 on windows? Manuel Reimer 05-20-2008
Posted by Manuel Reimer on May 20, 2008, 1:33 am
Please log in for more thread options
Hello,

I tried to forward input, output and error of a command to files using
open3 on windows.

I used the following code:

open(F_IN, "<$f_in");
open(F_OUT, ">$f_out");
open(F_ERR, ">$f_err");
my $pid = open3(\*F_IN, \*F_OUT, \*F_ERR, "$command");
waitpid($pid, 0);

Anything, this code does, is hanging on waitpid.

Does open3 work on windows? If yes: How can I use it to forward all
input/output channels to files?

Thanks in advance

CU

Manuel


Posted by Ben Morrow on May 20, 2008, 6:54 am
Please log in for more thread options

Quoth Manuel.Spam@nurfuerspam.de:
>
> I tried to forward input, output and error of a command to files using
> open3 on windows.
>
> I used the following code:
>
> open(F_IN, "<$f_in");
> open(F_OUT, ">$f_out");
> open(F_ERR, ">$f_err");
> my $pid = open3(\*F_IN, \*F_OUT, \*F_ERR, "$command");

open3 doesn't work like this: it opens the handles for you.

Under sane versions of windows (Win2k and later) you can use

system "$command <$f_in >$f_out 2>$f_err";

just as under Unix. Alternatively, you can use IPC::Run, which I would
recommend.

Ben

--
Many users now operate their own computers day in and day out on various
applications without ever writing a program. Indeed, many of these users
cannot write new programs for their machines...
-- F.P. Brooks, 'No Silver Bullet', 1987 [ben@morrow.me.uk]

Posted by zentara on May 20, 2008, 8:45 am
Please log in for more thread options
On Tue, 20 May 2008 07:33:37 +0200, Manuel Reimer

>Hello,
>
>I tried to forward input, output and error of a command to files using
>open3 on windows.
>
>I used the following code:
>
>open(F_IN, "<$f_in");
>open(F_OUT, ">$f_out");
>open(F_ERR, ">$f_err");
>my $pid = open3(\*F_IN, \*F_OUT, \*F_ERR, "$command");
>waitpid($pid, 0);
>
>Anything, this code does, is hanging on waitpid.
>
>Does open3 work on windows? If yes: How can I use it to forward all
>input/output channels to files?
>
>Thanks in advance

open3 uses pipes to open the filehandles, but Win32 dosn't
handle pipes well,

Use IPC::Run or IPC::Cmd on Win32, it uses sockets instead of pipes,
and win32 can handle it.

See
http://perlmonks.org?node_id=593769

for other ideas.


zentara

--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html

Posted by szr on May 20, 2008, 1:10 pm
Please log in for more thread options
zentara wrote:
> On Tue, 20 May 2008 07:33:37 +0200, Manuel Reimer
>
>> Hello,
>>
>> I tried to forward input, output and error of a command to files
>> using open3 on windows.
>>
>> I used the following code:
>>
>> open(F_IN, "<$f_in");
>> open(F_OUT, ">$f_out");
>> open(F_ERR, ">$f_err");
>> my $pid = open3(\*F_IN, \*F_OUT, \*F_ERR, "$command");
>> waitpid($pid, 0);
>>
>> Anything, this code does, is hanging on waitpid.
>>
>> Does open3 work on windows? If yes: How can I use it to forward all
>> input/output channels to files?
>>
>> Thanks in advance
>
> open3 uses pipes to open the filehandles, but Win32 dosn't
> handle pipes well,

Could you please elaborate on what you mean by, "doesn't[sic] handle
pipes well" ?


I find under 2000 and XP I can use pipes just as I can in a Linux/UNIX
environment.

E.G. :

ls -l | egrep "^d" | sort
some_program_with_lots_of_output | more
grep "1.2.3.4" access_log | tail -n 25
ls -l long_dir | less

(Note that some of these examples use Win32 ports of Linux uilities like
grep, sort, ls, tail, less... etc, which I use on all my Windows systems
so I can things in a similar manner on either win or nix :-) )

--
szr



Posted by zentara on May 21, 2008, 7:58 am
Please log in for more thread options

>zentara wrote:

>>>
>>> Does open3 work on windows? If yes: How can I use it to forward all
>>> input/output channels to files?
>>>
>>> Thanks in advance
>>
>> open3 uses pipes to open the filehandles, but Win32 dosn't
>> handle pipes well,
>
>Could you please elaborate on what you mean by, "doesn't[sic] handle
>pipes well" ?

In win32, select won't work reliably on pipes, and you need select's
can_read method to know when there is output available in the pipe.

>
>I find under 2000 and XP I can use pipes just as I can in a Linux/UNIX
>environment.
>
>E.G. :
>
> ls -l | egrep "^d" | sort
> some_program_with_lots_of_output | more
> grep "1.2.3.4" access_log | tail -n 25
> ls -l long_dir | less
>
>(Note that some of these examples use Win32 ports of Linux uilities like
>grep, sort, ls, tail, less... etc, which I use on all my Windows systems
>so I can things in a similar manner on either win or nix :-) )

Those are pipes used in a different context, since the commands close,
and thus flush the pipe.

You can use pipes, but IPC::Open3 needs select to know when the
pipes have readable data.
If you watch carefully, you may see IPC::Open3's output when you close
the program or filehandle, which will cause it to flush.

See
http://perlmonks.org?node_id=621058

for an explanation.

The is the Win32::Pipe and Win32::IPC modules which help.

zentara


--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html

Similar ThreadsPosted
IPC::Open3 August 24, 2005, 2:23 pm
IPC::Open3 November 11, 2006, 8:10 pm
How do you use IPC::Open3.... January 19, 2008, 5:11 pm
open3 and signals September 20, 2004, 8:43 pm
IPC::Open3 issue August 24, 2005, 3:21 pm
can't get STDERR using open3... December 20, 2006, 3:21 pm
IPC::open3: What goes wrong? August 1, 2007, 1:05 pm
Open3 with nonblocking reads September 3, 2004, 7:51 am
IPC::Open3 and the error filehandle October 26, 2007, 9:33 am
Scary IPC::Open3 filehandle difference October 28, 2004, 1:23 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap