Glib::IO watcher unbuffered?


I would like to have unbuffered output processing from an external command
(command prints a line, sleeps, prints a line, sleeps ...):

$| = 1;
open my $pipe, "$cmd |"
        or croak("Failed running $cmd: $!\n");


my $watcher;
$watcher = Glib::IO->add_watch( fileno( $pipe ), ['in', 'hup'],
                sub { watcher_callback($self, $pipe, $watcher); } );

sub watcher_callback {
    my ($self, $pipe, $watcher) = @_;

    $| = 1;

    if ( eof( $pipe ) ) {
        Gtk2::Helper->remove_watch( $watcher );
        close( $pipe );
    else {
        my $line = <$pipe>;
        print $line;        # printed with a delay, still buffering?

    return 1;

I tried to have $line processed without delay (autoflush all handles
everywhere...), but the watcher only prints bunches of lines after some longer
Why does the buffering happen and how to turn it off?

Thanks, Phil

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]