in reply to How do I close a pipe

The piped open returns the pid of the child process. So just kill it once you're done with it:

#!/usr/bin/perl $SIG{INT} = \&end; my $kid = open (TELNET, '-|', "telnet 192.168.1.1 6088"); while(<TELNET>) { $arr = <TELNET>; $arr =~ s/[\r\n](.....):(.*)/<p class="$1">$1:$2<\/p>/; print $arr; } end; sub end { print "Closing the session ...\n" # close FH; close TELNET; kill 2, $kid; ## Or whatever signal is appropriate! print "Session closed!\n" exit; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: How do I close a pipe
by gmoque (Acolyte) on Dec 16, 2008 at 02:15 UTC

    Thanks BrowserUk

    I thought that by closing the handler it will automatically end the child process, but I ended up the other way around, I killed the child process an I don't even need to close the handle, this is how I worked out:

    #!/usr/bin/perl $SIG{INT} = \&end; my $kid = open (TELNET, '-|', "telnet 192.168.1.1 6088"); while(<TELNET>) { $arr = <TELNET>; $arr =~ s/[\r\n](.....):(.*)/<p class="$1">$1:$2<\/p>/; print $arr; } end; sub end { kill 9, $kid; ## Or whatever signal is appropriate! print "Session closed!\n" exit; }

    But actually I am moving forward now, this version didn't work on Active Perl, I am looking for something that would support as much platforms as I can.

    Now instead of use the pipe I use the IO::Socket::INET library with the recv subroutine, but since it is lower level I am having some troubles parsing the lines :P.

    Anyways thanks for the help, now this script is helping me a lot for my work.

    gmoque