Re: help using multiple processes in a ptk program

Slaven Rezic <[email protected]> Sat, 20 Mar 2010 18:26:46 +0100
Newsgroups gmane.comp.lang.perl.tk
Message-ID <[email protected]>
[email protected] writes:

> Hello, 
>
> I am trying to set up a perl/tk program which needs to have a label
> showing whether my ssh server is available or not. I would like not to
> use threads, but instead multiple processes. I tried to combine
> a few receipes from "Mastering Perl/Tk" by Steve Lidie and Nancy Walsh
> and "Programming Perl" by Larry Wall et al. Somehow there must be a
> problem of scoping here, which I somehow overlook:
>
>
> #!/usr/bin/perl -w
>
> use Tk;
> use Net::Ping;
> my $is_available;
> my $ssh_host = "192.168.2.1";
>
> sub fill_available_widget {
>     $is_available = <IS_AVAILABLE>;
> }
>
>
> if (open(IS_AVAILABLE, "-|")) {
>     my $mw = MainWindow->new;
>     $mw->title("Hello");
>     my $label = $mw->Label(-textvariable => \$is_available)->pack;
>     $mw->fileevent(\*IS_AVAILABLE, 'readable',
> 		   [\&fill_available_widget, $label]); 
>     $mw->Button(-text => "done", -command => sub {exit})->pack;
>     $mw->bind('<q>', sub {exit});
>     MainLoop;
> }
> else {
> 	my $p = Net::Ping->new();
> 	if ($p->ping($ssh_host)) {
> 	    print STDOUT "not available\n" }
> 	else {
> 	    print STDOUT "available\n"
> 	};
> 	$p->close();
> 	exit;
> };
>
>
> Does anybody have an idea?
>

You have to remove the fileevent handler in case of a end-of-file
condition. Roughly:

    sub fill_available_widget {
        my $label = shift;
        if (eof(IS_AVAILABLE)) {
            $label->fileevent(\*IS_AVAILABLE, 'readable', '');
            return;
        }
        $is_available = <IS_AVAILABLE>;
    }

Regards,
    Slaven

-- 
Slaven Rezic - slaven <at> rezic <dot> de

    tksm - Perl/Tk program for searching and replacing in multiple files
    http://ptktools.sourceforge.net/#tksm
--++**==--++**==--++**==--++**==--++**==--++**==--++**==
ptk mailing list
[email protected]
https://mailman.stanford.edu/mailman/listinfo/ptk