Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I close a pipe

by gmoque (Acolyte)
on Dec 14, 2008 at 04:55 UTC ( [id://730249]=perlquestion: print w/replies, xml ) Need Help??

gmoque has asked for the wisdom of the Perl Monks concerning the following question:

Monks:

Does anyone knows how to close a pipe catching the event using SIG{INT}? When I run the code below and I send Ctrl+C I go into the sub end but it looks like the telnet session never closes.*

Here is my code:

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

Any suggestions so the Telnet session won't hung up?

*NOTE: The telnet session is done to a terminal that just outputs text (log), I don't have to login or type anything after opening the telnet.

Thanks

Replies are listed 'Best First'.
Re: How do I close a pipe
by BrowserUk (Patriarch) on Dec 14, 2008 at 07:15 UTC

    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.

      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

Re: How do I close a pipe
by f00li5h (Chaplain) on Dec 14, 2008 at 04:58 UTC

    open (TELNET, '-|', "telnet 192.168.1.1 6088");

    close FH;

    These refer to different file handles. If you use lexical file handles (open my $telnet, ...) your file handle will be closed by magic when it drops out of scope

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

      I am sorry about the FH name, I updated the post.

      I added the SIG{INT} trap because without it when I send the signal (CTRL+C) the terminal session stays running, do you think that by closing the file handle (pipe) the telnet session is not being closed properly? (The server will never send EOF since it is always loggin data to the output)

        Per close, after the pipe is closed, perl will wait for the telnet process to complete and return its exit status in $?. Closing the pipe will result in telnet getting a SIGPIPE, which should cause telnet to terminate, but YMMV.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://730249]
Approved by graff
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-10-04 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (42 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.