Question

I read in the perldoc that the -t file operator is used to decide if a filehandle is opened to a tty or not. Then i read what a tty was and from what i understand it's an old term for a terminal?

My main question however is what this code is used for:

if(-t STDERR) {
    die "some warning/error message here"
}

I guess i don't really understand what it means for a "filehandle to be opened to a tty". Does that mean that the particular output for that filehandle appears on the tty - or the terminal? Can tty refer to something besides the terminal?

Was it helpful?

Solution 2

This code wants to assert that standard error is being redirected to a file. So running

perl the_script.pl

would trigger the die call, but

perl the_script.pl 2> error.log

would not.

OTHER TIPS

TTYs are a type of unix device. Consoles appear as TTYs to programs, so that statement basically reads as

"If STDERR is sent to a console, ..."

But not quite. We're not talking about physical devices but logical devices such as /dev/tty*. As such, it's possible for a program to pretend to be a terminal. For example, Expect uses IO::Pty to do this.

STDERR could also be a plain file, a pipe or a socket. -t would be false for these.

script 2>foo        # Plain file
script 2>&1 | cat   # Pipe
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top