| 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 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | package Tee; | ||||||||||
| 13 | 4 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | $Tee::VERSION = '0.14'; | |||||||||
| 15 | } | ||||||||||
| 16 | # ABSTRACT: Pure Perl emulation of GNU tee | ||||||||||
| 17 | |||||||||||
| 18 | 4 | ▶ | use Exporter (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 4 | ▶ | use Probe::Perl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 4 | our @ISA = qw (Exporter); | |||||||||
| 22 | 4 | our @EXPORT = qw (tee); | |||||||||
| 23 | |||||||||||
| 24 | #--------------------------------------------------------------------------# | ||||||||||
| 25 | # Platform independent ptee invocation | ||||||||||
| 26 | #--------------------------------------------------------------------------# | ||||||||||
| 27 | |||||||||||
| 28 | 4 | my $perl = Probe::Perl->find_perl_interpreter; | |||||||||
| 29 | 4 | my $ptee_cmd = "$perl -MTee::App -e run --"; | |||||||||
| 30 | |||||||||||
| 31 | #--------------------------------------------------------------------------# | ||||||||||
| 32 | # Functions | ||||||||||
| 33 | #--------------------------------------------------------------------------# | ||||||||||
| 34 | |||||||||||
| 35 | 4 | ▶ | sub tee { | ||||||||
|
Subroutine
tee: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 36 | 4 | my $command = shift; | |||||||||
| 37 | 4 | my $options; | |||||||||
| 38 | 4 | ▶ | $options = shift if (ref $_[0] eq 'HASH'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 4 | my $files = join(" ", @_); | |||||||||
| 40 | 4 | ▶ | my $redirect = $options->{stderr} ? " 2>&1 " : q{}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 4 | ▶ | my $append = $options->{append} ? " -a " : q{}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 4 | system( "$command $redirect | $ptee_cmd $append $files" ); | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 4 | 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<ptee> 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<ptee> 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<ptee> (default: false) | ||||||||||
| 103 | |||||||||||
| 104 | =item * | ||||||||||
| 105 | |||||||||||
| 106 | append -- passes the C<<< -a >>> flag to L<ptee> 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<IPC::Run3>, 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<ptee> | ||||||||||
| 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<http://rt.cpan.org/Dist/Display.html?Queue=Tee> | ||||||||||
| 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 <dagolden@cpan.org> | ||||||||||
| 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__ | ||||||||||