File Coverage

blib/lib/Tee.pm
Criterion Covered Total %
statement 20 20 100.0
branch 6 6 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 33 33 100.0


line stmt bran cond sub pod time code
1             #
2             # This file is part of Tee
3             #
4             # This software is Copyright (c) 2006 by David Golden.
5             #
6             # This is free software, licensed under:
7             #
8             # The Apache License, Version 2.0, January 2004
9             #
10 3     3   6886 use strict;
  3         6  
  3         117  
11 3     3   19 use warnings;
  3         6  
  3         149  
12             package Tee;
13             BEGIN {
14 3     3   63 $Tee::VERSION = '0.14';
15             }
16             # ABSTRACT: Pure Perl emulation of GNU tee
17              
18 3     3   17 use Exporter ();
  3         5  
  3         56  
19 3     3   907 use Probe::Perl;
  3         1870  
  3         683  
20              
21             our @ISA = qw (Exporter);
22             our @EXPORT = qw (tee);
23              
24             #--------------------------------------------------------------------------#
25             # Platform independent ptee invocation
26             #--------------------------------------------------------------------------#
27              
28             my $perl = Probe::Perl->find_perl_interpreter;
29             my $ptee_cmd = "$perl -MTee::App -e run --";
30              
31             #--------------------------------------------------------------------------#
32             # Functions
33             #--------------------------------------------------------------------------#
34              
35             sub tee {
36 4     4 1 47582 my $command = shift;
37 4         15 my $options;
38 4 100       24 $options = shift if (ref $_[0] eq 'HASH');
39 4         13 my $files = join(" ", @_);
40 4 100       21 my $redirect = $options->{stderr} ? " 2>&1 " : q{};
41 4 100       22 my $append = $options->{append} ? " -a " : q{};
42 4         323033 system( "$command $redirect | $ptee_cmd $append $files" );
43             }
44              
45             1; # modules must be true
46              
47              
48              
49             =pod
50              
51             =head1 NAME
52              
53             Tee - Pure Perl emulation of GNU tee
54              
55             =head1 VERSION
56              
57             version 0.14
58              
59             =head1 SYNOPSIS
60              
61             # from Perl
62             use Tee;
63             tee( $command, @files );
64            
65             # from the command line
66             $ cat README.txt | ptee COPY.txt
67              
68             =head1 DESCRIPTION
69              
70             The C<<< Tee >>> distribution provides the L program, a pure Perl emulation of
71             the standard GNU tool C<<< tee >>>. It is designed to be a platform-independent
72             replacement for operating systems without a native C<<< tee >>> program. As with
73             C<<< tee >>>, it passes input received on STDIN through to STDOUT while also writing a
74             copy of the input to one or more files. By default, files will be overwritten.
75              
76             Unlike C<<< tee >>>, C<<< ptee >>> does not support ignoring interrupts, as signal handling
77             is not sufficiently portable.
78              
79             The C<<< Tee >>> module provides a convenience function that may be used in place of
80             C<<< system() >>> to redirect commands through C<<< ptee >>>.
81              
82             =head1 USAGE
83              
84             =head2 C<<< tee() >>>
85              
86             tee( $command, @filenames );
87             tee( $command, \%options, @filenames );
88              
89             Executes the given command via C<<< system() >>>, but pipes it through L to copy
90             output to the list of files. Unlike with C<<< system() >>>, the command must be a
91             string as the command shell is used for redirection and piping. The return
92             value of C<<< system() >>> is passed through, but reflects the success of
93             the C<<< ptee >>> command, which isn't very useful.
94              
95             The second argument may be a hash-reference of options. Recognized options
96             include:
97              
98             =over
99              
100             =item *
101              
102             stderr -- redirects STDERR to STDOUT before piping to L (default: false)
103              
104             =item *
105              
106             append -- passes the C<<< -a >>> flag to L to append instead of overwriting
107             (default: false)
108              
109             =back
110              
111             =head1 LIMITATIONS
112              
113             Because of the way that C<<< Tee >>> uses pipes, it is limited to capturing a single
114             input stream, either STDOUT alone or both STDOUT and STDERR combined. A good,
115             portable alternative for capturing these streams from a command separately is
116             L, though it does not allow passing it through to a terminal at the
117             same time.
118              
119             =head1 SEE ALSO
120              
121             =over
122              
123             =item *
124              
125             L
126              
127             =item *
128              
129             IPC::Run3
130              
131             =item *
132              
133             IO::Tee
134              
135             =back
136              
137             =head1 BUGS
138              
139             Please report any bugs or feature using the CPAN Request Tracker.
140             Bugs can be submitted through the web interface at
141             L
142              
143             When submitting a bug or request, please include a test-file or a patch to an
144             existing test-file that illustrates the bug or desired feature.
145              
146             =head1 AUTHOR
147              
148             David Golden
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is Copyright (c) 2006 by David Golden.
153              
154             This is free software, licensed under:
155              
156             The Apache License, Version 2.0, January 2004
157              
158             =cut
159              
160              
161             __END__