写入 STDOUT 和打开到“/dev/tty"的文件句柄有什么区别? [英] What is the difference between writing to STDOUT and a filehandle opened to "/dev/tty"?

查看:130
本文介绍了写入 STDOUT 和打开到“/dev/tty"的文件句柄有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个例子有什么区别?

What are the differences between this two examples?

#!/usr/bin/perl
use warnings;
use 5.012;
my $str = "\x{263a}";


open my $tty, '>:encoding(utf8)', '/dev/tty' or die $!;
say $tty $str;
close $tty;

open $tty, '>:bytes', '/dev/tty' or die $!;
say $tty $str;
close $tty;

# -------------------------------------------------------

binmode STDOUT, ':encoding(utf8)' or die $!;
say $str;

binmode STDOUT, ':bytes' or die $!;
say $str;

推荐答案

不同之处在于您正在写入两个不同的和(从 Perl 和您的程序的角度来看)独立 文件句柄.

The difference is that you are writing to two distinct and (from Perl's and your program's point of view) independent file handles.

  • 第一个是在 Unixy OS 上打开一个特殊设备"文件的文件句柄,它是进程控制终端的同义词,如果有"(引用自此 Linux 文档).请注意,虽然它通常被认为是屏幕",但它不一定是(例如,该终端可以链接到串行端口的设备文件);它可能不存在或无法打开.

  • The first one is a file handle opened to a special "device" file on Unixy OS which is "a synonym for the controlling terminal of a process, if any" (quote from this Linux document). Please note that while it is commonly thought of as "screen", it doesn't have to be (e.g. that terminal could be linked to a serial port's device file instead); and it may not exist or not be openable.

第二个是默认处理的与进程的文件描述符 #1 相关联的文件.

The second one is a file handled associated by default with file descriptor #1 for the process.

它们乍一看似乎是相同的,因为在典型情况下,Unix shell 默认将其文件描述符 #1(因此它启动的每个进程之一)与 /dev/tty.

They may SEEM to be identical at first glance due to the fact that, in a typical situation, a Unix shell will by default associate its file descriptor #1 (and thus one of every process it launches without redirects) with /dev/tty.

从 Perl 的角度来看,两者没有任何共同点,除了由于 Unix shell 的工作方式,这两者通常默认关联的事实.

The two have nothing in common from Perl point of view, OTHER than the fact that those two commonly end up associated by default due to the way Unix shells work.

由于这种默认设置,这两个引用的代码段的功能行为通常看起来相同,但这只是偶然".

The functional behavior of the two quoted pieces of code will often SEEM identical due to this default, but that is just "by accident".

在实际差异中:

  • /dev/tty 不一定存在于非 Unixy 操作系统上.因此,使用 tty 是非常不可移植的.Windows 等效于 CON: IIRC.

  • /dev/tty does not necessarily exist on non-Unixy OSs. It's therefore highly un-portable to use tty. Windows equivalent is CON: IIRC.

STDOUT 程序的调用者可以关联(重定向)到任何东西.可以关联到一个文件,可以是另一个进程的标准输入的管道.

STDOUT of a program can be associated (re-directed) to ANYTHING by whoever called the program. Could be associated to a file, could be a pipe to another process's STDIN.

您可以使用 -t 检查您的 STDOUT 是否已连接到 tty 运算符:

You can CHECK whether your STDOUT is connected to a tty by using the -t operator:

if ( -t STDOUT ) { say 'STDOUT is connected to a tty' }

另一方面,请注意您可以通过显式关闭 STDOUT 文件句柄并重新打开它以指向 /dev/来确保您的 STDOUT 写入 /dev/ttytty:

As another aside, please note that you CAN make sure that your STDOUT writes to /dev/tty by explicitly closing the STDOUT filehandle and re-opening it to point to /dev/tty:

close STDOUT or die $!;
open STDOUT '>:encoding(utf8)', '/dev/tty' or die $!;

这篇关于写入 STDOUT 和打开到“/dev/tty"的文件句柄有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆