[sf-perl] determining console or xwindows mode
Joe Brenner
doom at kzsu.stanford.edu
Sun Feb 28 22:14:49 PST 2010
Richard Reina <gatorreina at gmail.com> wrote:
> Quinn Weaver <quinn at fairpath.com>
>
> > Richard Reina <gatorreina at gmail.com> wrote:
> >> I have a program that must run differently in xwindows then it
> >> would on a linux console. I was wondering if there's a function to
> >> determine if a program is running in a linux console or in xwindows
> > If you're running under X, the DISPLAY environment variable should be set.
> > So do:
> >
> > if ($ENV{DISPLAY}) {
> > ...
>
> Perfect. Thank you. It looks like both suggestions will work.
Also, you might want to look at the Perl Cookbook, 2nd edition,
recipe 15.2 "Testing Whether a Progam Is Running Interactively"
They suggest:
use POSIX qw( getpgrp tcgetpgrp );
sub i_am_interactive {
my $tty;
open( $tty, '<', "/dev/tty") or die "$!";
my $tpgrp = tcgetpgrp( fileno( $tty ) );
my $pgrp = getpgrp();
close $tty;
return ($tpgrp == $pgrp);
}
Or, in the increasingly unlikely event that you're not
on a POSIX system:
sub i_am_interactive {
return -t STDIN && -t STDOUT;
}
More information about the SanFrancisco-pm
mailing list