Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6040373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:30:17+00:00 2026-05-23T06:30:17+00:00

Assuming a handle created with the following code: use IO::File; my $fh = IO::File->new;

  • 0

Assuming a handle created with the following code:

use IO::File;

my $fh = IO::File->new;

my $pid = $fh->open('some_long_running_proc |') or die $!;

$fh->autoflush(1);
$fh->blocking(0);

and then read with a loop like this:

while (some_condition_here) {
    my @lines = $fh->getlines;
    ...
    sleep 1;
}

What do I put as some_condition_here that will return false if the process on the other end of the pipe has terminated?

Testing for $fh->eof will not work since the process could still be running without printing any new lines. Testing for $fh->opened doesn’t seem to do anything useful.

Currently I am using $pid =! waitpid($pid, WNOHANG) which seems to work in POSIX compliant environments. Is this the best way? What about on Windows?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:30:18+00:00Added an answer on May 23, 2026 at 6:30 am

    On using select,

    use strict;
    use warnings;
    
    use IO::Select qw( );
    
    sub process_msg {
        my ($client, $msg) = @_;
        chomp $msg;
        print "$client->{id} said '$msg'\n";
        return 1;  # Return false to close the handle.
    }
    
    my $select = IO::Select->new();
    my %clients;
    
    for (...) {
        my $fh = ...;
        $clients{fileno($fh)} = {
            id  => '...'
            buf => '',
            # ...
        };
    
        $select->add($fh);
    }
    
    while (my @ready = $select->can_read) {
        for my $fh (@ready) {
            my $client = $clients{ fileno($fh) };
            our $buf; local *buf = \( $client->{buf} );
    
            my $rv = sysread($fh, $buf, 64*1024, length($buf));
            if (!$rv) {
                if (defined($rv)) {
                    print "[$client->{id} ended]\n";
                } else {
                    print "[Error reading from $client->{id}: $!]\n";
                }
    
                print "[Incomplete message received from $client->{id}]\n"
                    if length($buf);
    
                delete $clients{ fileno($fh) };
                $select->remove($fh);
                next;
            }
    
            while ($buf =~ s/^(.*\n)//) {
                if (!process_msg($client, "$1")) {
                    print "[Dropping $client->{id}]\n";
                    delete $clients{ fileno($fh) };
                    $select->remove($fh);
                    last;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming that I have the following code: final Catalog catalog = createCatalog(); for (int
I am assuming nhibernate can handle circular reference issues as I have not seen
I have the following code (OLE = OptimisticLockException) ... public void outer() { try
When a TForm is created in Builder (or Delphi) code is added to main
I've created my own application in VB.NET that saves its documents into a file
I have the following code to do a postback to a remote URL: request
I am completely new to Java and have an assignment coming up; with the
(Assuming a WCF method called MyFunction) Currently, to support canceling a WCF request, I'm
I am going to create a new web application that is very customized. It
I am looking at code that toggles displaying and hiding a table column. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.