Home > Software engineering >  -t in the body of a perl script
-t in the body of a perl script

Time:01-20

I have a script with a line like this:

$foo = $bar if -t;

Near as I can tell, this is saying,

  1. if this script is run from a terminal, set $foo to $bar.

  2. If this script was run from cron, that would evaluate to false.

Have I got this right?

CodePudding user response:

In perldoc perlfunc for the collection of functions called -X, you can read:

-t  Filehandle is opened to a tty.

Also

If the argument is omitted, tests $_, except for -t, which tests STDIN. 

Which is to say your code does -t STDIN.

CodePudding user response:

The -t file test is documented in perlfunc, although you get to by looking up -X instead of the specific file test:

% perldoc -f -X

Depending on your task, IO::Interactive may do the job better since there can be a few gotchas with figuring out if something is truly interactive.

If you want to know that you are running under cron (and not non-interactive in some other way), you might consider have a variable set in your crontab (or using one already set) and simply looking for it. In your crontab:

IN_CRON=1

Then, in the script:

do_something() if $ENV{IN_CRON};
  •  Tags:  
  • Related