in reply to X or console?

This isn't perl but it's a reasonably comprehensive test for X. You could just go straight to xdpyinfo but then you have to wait for it to time out if no x server is present. It's bash syntax and was provided to me by an instructor on a training course (thanks Boyd!), conversion to perl left as an exercise for the reader :)
declare displayhost=${DISPLAY%:*} declare pingcmd case $OSTYPE in solaris*) pingcmd="ping $displayhost 1";; linux*) pingcmd="ping -c 1 $displayhost";; *) : ;; # TODO insert other flavors of ping here esac # If displayhost is blank DISPLAY is probably ":0" so we # shouldn't ping [[ -z "$displayhost" ]] && pingcmd="" if ! ( $pingcmd && xdpyinfo ) >/dev/null 2>&1; then # Either we can't ping the machine or xdpyinfo failed. # Either way, X is probably not going to work! unset _running_X fi

--
Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho