[perl #38901] perlfaq8 correction
[email protected] ("Steve Peters via RT") Wed, 19 Apr 2006 09:35:10 -0700
| Newsgroups | perl.perlfaq.workers,perl.perl5.porters |
|---|---|
| Message-ID | <[email protected]> |
> [[email protected] - Wed Apr 12 01:27:26 2006]: > > > This is a bug report for perl from [email protected], > generated with the help of perlbug 1.35 running under perl v5.8.7. > > > ----------------------------------------------------------------- > About "How do I find out if I’m running interactively or not?": > > The snippet with the solution for POSIX systems emits a die() if > /dev/tty cannot be opened. I think it should not die but rather > print "no tty". The situation that there's no /dev/tty can happen > on boot time on Linux systems. > > So my suggestion is: > > use POSIX qw/getpgrp tcgetpgrp/; > if (!open(TTY, "/dev/tty")) { > print "no tty\n"; > } else { > $tpgrp = tcgetpgrp(fileno(*TTY)); > $pgrp = getpgrp(); > if ($tpgrp == $pgrp) { > print "foreground\n"; > } else { > print "background\n"; > } > } > > Regards, > Slaven > Your suggestion has been added to the FAQ with the following change. Change 27905 by stevep@stevep-mccoy on 2006/04/19 16:32:26 Changes to perlfaq8 "How do I find out if I'm running interactively or not?" suggested by Slaven Rezic in RT #38901: perlfaq8 correction Affected files ... ... //depot/perl/pod/perlfaq8.pod#50 edit Differences ... ==== //depot/perl/pod/perlfaq8.pod#50 (text) ==== @@ -1005,13 +1005,19 @@ the current process group of your controlling terminal as follows: use POSIX qw/getpgrp tcgetpgrp/; - open(TTY, "/dev/tty") or die $!; - $tpgrp = tcgetpgrp(fileno(*TTY)); - $pgrp = getpgrp(); - if ($tpgrp == $pgrp) { - print "foreground\n"; + + # Some POSIX systems, such as Linux, can be + # without a /dev/tty at boot time. + if (!open(TTY, "/dev/tty")) { + print "no tty\n"; } else { - print "background\n"; + $tpgrp = tcgetpgrp(fileno(*TTY)); + $pgrp = getpgrp(); + if ($tpgrp == $pgrp) { + print "foreground\n"; + } else { + print "background\n"; + } } =head2 How do I timeout a slow event?