Glib is cross-platform, and here is a good example of using it with a Pty.

And yes, the pipe reading thru Glib's methods works on Win32.

#!/usr/bin/perl use warnings; use strict; # by Phil Letschert on gtk2-perl maillist #It is common behaviour that the C library uses isatty(3) to decide if + stdout #is line-buffered or block-buffered. When reading from a pipe to an ex +ternal #command the output is block-buffered, which explains the reported beh +aviour. #Solution is using a pseudo-terminal and connect the watcher to it, fo +llowing is #a complete example: use Glib; use IO::Pty; my $pty = IO::Pty->new; my $cmd = "top -d .5"; my $pid; unless ($pid = fork) { # child process die "problem spawning program: $!\n" unless defined $pid; use POSIX (); POSIX::setsid or die "setsid failed: $!"; my $tty = $pty->slave; my $tty_fd = $tty->fileno; open STDIN, "<&$tty_fd" or die $!; open STDOUT, ">&$tty_fd" or die $!; open STDERR, ">&STDOUT" or die $!; close $pty; close $tty; exec $cmd; } my $main_loop = Glib::MainLoop->new; my $watcher; $watcher = Glib::IO->add_watch( fileno( $pty ), ['in', 'hup'], \&callb +ack); $main_loop->run; sub callback { if ( $pty->eof ) { Gtk2::Helper->remove_watch( $watcher ); close( $pty ); } else { my $line = $pty->getline; print $line; } }
If you need to read and write to it, look at this usage: (only quickly tested :-) )
#!/usr/bin/perl use warnings; use strict; use Glib; use IO::Pty; my $pty = IO::Pty->new; my $cmd = "top"; my $pid; unless ($pid = fork) { # child process die "problem spawning program: $!\n" unless defined $pid; use POSIX (); POSIX::setsid or die "setsid failed: $!"; my $tty = $pty->slave; my $tty_fd = $tty->fileno; open STDIN, "<&$tty_fd" or die $!; open STDOUT, ">&$tty_fd" or die $!; open STDERR, ">&STDOUT" or die $!; exec $cmd; } my $main_loop = Glib::MainLoop->new; my $watcher; $watcher = Glib::IO->add_watch( fileno( $pty ), ['in', 'hup'], \&callb +ack); syswrite( $pty, 'l',16); syswrite( $pty, "\n",16); syswrite( $pty, 't',16); syswrite( $pty, "\n",16); syswrite( $pty, 'm',16); syswrite( $pty, "\n",16); $main_loop->run; sub callback { if ( $pty->eof ) { Gtk2::Helper->remove_watch( $watcher ); close( $pty ); } else { my $line = $pty->getline; print $line; } }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

In reply to Re^3: Windows alternative to open2() or IO::Pty by zentara
in thread Windows alternative to IO::Pty by azredwing

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.