Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the -t used for in this Perl code

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?

like image 540
rage Avatar asked Dec 16 '22 03:12

rage


1 Answers

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
like image 127
ikegami Avatar answered Jan 13 '23 05:01

ikegami