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

Hello Monks,
I am writing a PerlTk application and would like to give the user a helpful message and simple text-only capability in the event that they do not have their $DISPLAY variable set correctly, or do not have X available at all.
Currently, this code:
use Tk; my $mw = MainWindow->new;
produces this output:
X connection to remotehost:10.0 broken (explicit kill or server shutdo +wn).
The trouble is, I can't seem to trap it with an "eval", the process exits anyway. The only option I see is to fork before the Tk code, and have the parent watch the output of the child, but I would like to avoid having an extra process hanging around. Are there any other options? What am I missing?
Thanks much!
-Nathan

Replies are listed 'Best First'.
Re: Handle Tk "X connection broken" gracefully?
by jasonk (Parson) on Feb 13, 2003 at 20:08 UTC

    This is the message you should get if you did manage to connect to the X server, but the window was then killed for some reason (using xkill on the window it creates will produce this message for example), you probably aren't trapping it because it's occuring sometime after your eval. This code does work if the display is wrong or X is not available...

    #!/usr/bin/perl -w use strict; use Tk; my $mw; eval { $mw = MainWindow->new; }; if($@) { print "Using text only\n"; undef $mw; } if($mw) { $mw->MainLoop; } else { print "Text version here\n"; }
      Thanks jasonk!
      I was getting that message when ssh'ing from a windows box to the linux box running the app. I guess that sshd accepted the connection, allowing the code to get past the eval, and then died when it couldn't connect to an X display on the windows box.
      I can live with that special case being broken, but it would be oh so cool if anyone has workarounds for it!
      Thanks!
      -Nathan
        Let's see if I get this... The windows box runs ssh (in command prompt window? via some windows app like SecureCRT?) to connect to a linux box and run a perl/tk app in linux, and you want the perl/tk window to show up on the windows screen. Is that it?

        There are X servers available for windows, which would allow this to work, but I don't know of any free ones off-hand (a google search is bound to turn up at least a couple). There is also a free (open-source?) package called "VNC", which would involve running a special "server" process on the linux box, and connecting to that server from the windows box -- it puts up a "client" window on your local machine and inside that window it's as if you're on an X display connected to the remote host -- you use something like a .xinitrc to launch whatever you want within that window on connection (xterm, etc). Pretty cool, but there is a lot of overhead that might make it go slower than you want. (Sorry not to do the google look-ups for you, but these are OT issues, after all...)