質問

I have a question about this answer, quoted below, by friedo to another question here. (I don't have permission to comment on it, so I am asking this as a question.)

"You can use File::Tee.

use File::Tee qw(tee);
tee STDOUT, '>>', 'some_file.out';
print "w00p w00p";

If File::Tee is unavailable, it is easily simulated with a pipeline:

open my $tee, "|-", "tee some_file.out";
print $tee "w00p w00p";
close $tee;

Are both of these tees the same? Or is one from Perl and the other from Linux/Unix?

役に立ちましたか?

解決

They're mostly the same, but the implementation details differ.

Opening a pipe to tee some_file.out forks a new process and runs the Unix / Linux utility program tee(1) in it. This program reads its standard input (i.e. anything you write to the pipe) and writes it both to some_file.out as well as to stdout (which it inherits from your program).

Obviously, this will not work under Windows, or on any other system that doesn't provide a Unix-style tee command.


The File::Tee module, on the other hand, is implemented in pure Perl, and doesn't depend on any external programs. However, according to its documentation, it also works by forking a new process and running what is essentially a Perl reimplementation of the Unix tee command under it. This does have some advantages, as the documentation states:

"It is implemeted around fork, creating a new process for every tee'ed stream. That way, there are no problems handling the output generated by external programs run with system or by XS modules that don't go through perlio."

On the other hand, the use of fork has its down sides as well:

"BUGS

Does not work on Windows (patches welcome)."


If you do want a pure Perl implementation of the tee functionality that works on all platforms, consider using IO::Tee instead. Unlike File::Tee, this module is implemented using PerlIO and does not use fork. Alas, this also means that it may not correctly capture the output of external programs executed with system or XS modules that bypass PerlIO.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top