in reply to X or console?
In general the easiest thing to do is try to talk to an X11 server. If you can't then you haven't got access to X.
I've not used X11 in Perl in anger, but the following should do the trick:
use X11::Protocol; my $display = $ENV{DISPLAY} || 'localhost:0'; my $has_x11 = eval { X11::Protocol->new($display) } ? 1 : 0;
While checking for $DISPLAY and an open port is better than nothing it can still give an incorrect result because that open port may not be an X11 server, and that X11 server may not allow you to connect even if it exists.
I presume whatever X API you're using has some kind of initialisation code - try it. If it succeeds you have access :-)
|
|---|