File-Tee-0.07/0000755000175000017500000000000011655006064012033 5ustar salvasalvaFile-Tee-0.07/META.yml0000664000175000017500000000077211655006064013314 0ustar salvasalva--- #YAML:1.0 name: File-Tee version: 0.07 abstract: ~ author: - Salvador Fandino license: unknown distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: {} no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 File-Tee-0.07/Changes0000644000175000017500000000201011654743365013332 0ustar salvasalvaRevision history for Perl extension File::Tee. 0.07 Nov 4, 2011 - set process name on child processes to indicate it is inside File::Tee - remove "only used once warning" - tests were failing on overloaded machines due to too tight timeouts 0.06 Jun 8, 2010 - ignore INT signal in forked processes 0.05 Jul 23, 2008 - print data to primary output file handled as soon as it becomes available (bug report by Samuel Wang) - add t/pods.t to MANIFEST 0.04 Dec 16, 2007 - <> was reading from @ARGV when present in children processes (bug reported by Daniel). 0.03 Oct 18, 2007 - new ignore_errors option added - new begin and end options added - new 'process => sub {}' option added - process option renamed to preprocess - require 5.6.x 0.02 Oct 17, 2007 - backport to 5.6.x 0.01 Mon Oct 15 10:55:24 2007 - original version; created by h2xs 1.23 with options -AXn File::Tee File-Tee-0.07/MANIFEST0000644000175000017500000000023111654742211013160 0ustar salvasalvaChanges Makefile.PL MANIFEST README t/File-Tee.t t/pods.t lib/File/Tee.pm META.yml Module meta-data (added by MakeMaker) File-Tee-0.07/t/0000755000175000017500000000000011655006064012276 5ustar salvasalvaFile-Tee-0.07/t/File-Tee.t0000644000175000017500000000414411654742702014065 0ustar salvasalva#!/usr/bin/perl use strict; use warnings; use Test::More tests => 50; use File::Tee qw(tee); open my $tfh, '>', 't/test_data' or die "unable to open test file"; select((select($tfh), $| = 1)[0]); open my $cfh, '>', 't/test_control' or die "unable to open test control file"; open my $cp4fh, '>', 't/test_copy_4'; select((select($cp4fh), $| = 1)[0]); open my $cp5fh, '>', 't/test_copy_5'; select((select($cp5fh), $| = 1)[0]); my @cap; ok(my $pid = tee($tfh, '>', 't/test_copy', 't/test_copy_2', { reopen => 't/test_copy_3' }, sub { print $cp4fh $_}, { process => sub { push @cap, $_ }, end => sub { print $cp5fh @cap } } )); my $out = ''; my $l = ''; for (0..10) { $l = "hello world ($_)\n"; $out .= $l; ok(print($tfh $l), "print $_ t"); kill INT => $pid; ok(print($cfh $l), "print $_ c"); } for $l ("missing end of line...", "more data...", "end of line\n") { chomp (my $l1 = $l); ok(print($tfh $l), "missing end of line - $l1"); ok(print($cfh $l), "missing end of line - $l1 c"); $out .= $l; sleep 3; ok(open my $meof, '<', 't/test_data'); { local $/; is(scalar(<$meof>), $out); } close($meof); } alarm 10; ok(close($tfh), "close tfh"); alarm 0; sleep 3; ok(open $tfh, '<', 't/test_data'); ok(open $cfh, '<', 't/test_control'); ok(open my $cpfh, '<', 't/test_copy'); ok(open my $cp2fh, '<', 't/test_copy_2'); ok(open my $cp3fh, '<', 't/test_copy_3'); ok(open $cp4fh, '<', 't/test_copy_4'); ok(open $cp5fh, '<', 't/test_copy_5'); { local $/; is(scalar(<$cfh>), $out, 'output $cfh'); is(scalar(<$tfh>), $out, 'output $tfh'); is(scalar(<$cpfh>), $out, 'output $cpfh'); is(scalar(<$cp2fh>), $out, 'output $cp2fh'); is(scalar(<$cp3fh>), $out, 'output $cp3fh'); is(scalar(<$cp4fh>), $out, 'output $cp4fh'); is(scalar(<$cp5fh>), $out, 'output $cp5fh'); } END { unlink 't/test_data'; unlink 't/test_control'; unlink 't/test_copy'; unlink 't/test_copy_2'; unlink 't/test_copy_3'; unlink 't/test_copy_4'; unlink 't/test_copy_5'; } File-Tee-0.07/t/pods.t0000644000175000017500000000047211654742211013433 0ustar salvasalva#!/usr/bin/perl use strict; use Test::More; plan skip_all => "Only the author needs to check that POD docs are right" unless eval "no warnings; getlogin eq 'salva'"; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok( all_pod_files( qw(blib) ) ); File-Tee-0.07/Makefile.PL0000644000175000017500000000030111654742211013777 0ustar salvasalva use 5.006; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'File::Tee', VERSION_FROM => 'lib/File/Tee.pm', AUTHOR => 'Salvador Fandino ' ); File-Tee-0.07/README0000644000175000017500000000113611654743477012733 0ustar salvasalvaFile-Tee version ================ This module is able to replicate the data written to a Perl stream to another stream(s). It is the Perl equivalent of the shell utility tee. INSTALLATION To install this module type the following: perl Makefile.PL make make test make install COPYRIGHT AND LICENCE Copyright (C) 2007, 2008, 2010, 2011 by Salvador Fandino This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. File-Tee-0.07/lib/0000755000175000017500000000000011655006064012601 5ustar salvasalvaFile-Tee-0.07/lib/File/0000755000175000017500000000000011655006064013460 5ustar salvasalvaFile-Tee-0.07/lib/File/Tee.pm0000644000175000017500000003321311655006015014531 0ustar salvasalvapackage File::Tee; our $VERSION = '0.07'; use strict; use warnings; no warnings 'uninitialized'; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(tee); use Carp; use Symbol qw(qualify_to_ref); use POSIX qw(_exit); use Fcntl qw(:flock); sub tee (*;@) { @_ >= 2 or croak 'Usage: tee($fh, $target, ...)'; my $fh = qualify_to_ref(shift, caller); my $last_mode; my @target; while (@_) { my $arg = shift @_; my %target; my %opts = ( ref $arg eq 'HASH' ? %$arg : ref $arg eq 'CODE' ? ( process => $arg ) : ( open => $arg ) ); $target{ignore_errors} = delete $opts{ignore_errors}; $target{prefix} = delete $opts{prefix}; $target{end} = delete $opts{end}; $target{begin} = delete $opts{begin}; $target{preprocess} = delete $opts{preprocess}; $target{process} = delete $opts{process}; unless (defined $target{process}) { $target{mode} = delete $opts{mode}; $target{open} = delete $opts{open}; $target{reopen} = delete $opts{reopen}; $target{autoflush} = delete $opts{autoflush}; $target{lock} = delete $opts{lock}; } %opts and croak "bad options '".join("', '", keys %opts)."'"; unless (defined $target{process}) { if (defined $target{reopen}) { croak "both 'open' and 'reopen' options used for the same target" if defined $target{open}; $target{open} = $target{reopen}; $target{reopen} = 1; } elsif (!defined $target{open}) { croak "missing mandatory argument 'open'"; } $target{autoflush} = 1 unless defined $target{autoflush}; $target{open} = [$target{open}] unless ref $target{open} eq 'ARRAY'; unless (defined $target{mode}) { if (ref $target{open}[0]) { if (ref $target{open}[0] eq 'CODE') { $target{mode} = 'CODE'; } else { $target{mode} = (defined $last_mode ? $last_mode : '>>&'); } } else { my ($mode, $fn) = shift(@{$target{open}}) =~ /^(\+?[<>]{1,2}(?:&=?)?|\|-?|)\s*(.*)$/; $mode = (defined $last_mode ? $last_mode : '>>') unless length $mode; $mode = '|-' if $mode eq '|'; unshift @{$target{open}}, $fn if length $fn; $target{mode} = $mode; } } $target{mode} =~ /^(?:>{1,2}&?|\|-|CODE)$/ or croak "invalid mode '$target{mode}'"; # file name is next argument or slurp everything when mode is '|-' unless (@{$target{open}} > 0) { if (ref $arg ne 'HASH' and @_) { if ($target{mode} eq '|-') { @{$target{open}} = splice @_; } else { my $last_mode = $target{mode}; @{$target{open}} = shift; } } else { croak "missing target file name"; } } $target{open}[0] = qualify_to_ref($target{open}[0], caller) if $target{mode} =~ tr/&//; unless ($target{mode} eq '|-') { open my $teefh, $target{mode}, @{$target{open}} or return undef; if ($target{reopen}) { $target{mode} =~ s/>+/>>/; close $teefh or return undef; } else { $target{teefh} = $teefh; if ($target{autoflush}) { my $oldsel = select $teefh; $| = 1; select $oldsel; } } } } push @target, \%target; } my $fileno = eval { fileno($fh) }; defined $fileno or croak "only real file handles can be tee'ed"; unless (defined $fileno) { return undef; } # flush any data buffered in $fh my $oldsel = select($fh); my @oldstate = ($|, $%, $=, $-, $~, $^, $.); $| = 1; select $oldsel; open my $out, ">&$fileno" or return undef; $oldsel = select $out; $| = $oldstate[0]; select $oldsel; my $pid = open $fh, '|-'; unless ($pid) { defined $pid or return undef; $SIG{INT} = 'IGNORE'; undef @ARGV; eval { $0 = "perl [File::Tee]" }; my $error = 0; my $oldsel = select STDERR; $| = 1; for my $target (@target) { my $begin = $target->{begin}; &$begin if $begin; } my $buffer = ''; my $eof; while(!$error and !$eof) { my $read = sysread STDIN, $buffer, 16*1024, length $buffer; if ($read) { print $out substr $buffer, -$read; } else { $eof = 1; } while (!$error and length $buffer) { my $line; my $eol = index $buffer, $/; if ($eol >= 0) { $line = substr $buffer, 0, $eol + length $/, ''; } elsif ($eof) { $line = $buffer; $buffer = ''; } else { last; } for my $target (@target) { my $cp = $line; $cp = join('', $target->{preprocess}($cp)) if $target->{preprocess}; $cp = $target->{prefix} . $cp if length $target->{prefix}; my $process = $target->{process}; if ($process) { my $ok; $ok = &$process for ($cp); $error = 1 unless ($ok or $target->{ignore_errors}); } else { my $teefh = $target->{teefh}; unless ($teefh) { undef $teefh; if (open $teefh, $target->{mode}, @{$target->{open}}) { unless ($target->{reopen}) { $target->{teefh} = $teefh; if ($target->{autoflush}) { my $oldsel = select $teefh; $| = 1; select $oldsel; } } } else { $error = 1 unless $target->{ignore_errors}; next; } } flock($teefh, LOCK_EX) if $target->{lock}; print $teefh $cp; flock($teefh, LOCK_UN) if $target->{lock}; if ($target->{reopen}) { unless (close $teefh) { $error = 1 unless $target->{ignore_errors}; } delete $target->{teefh}; } } } } } for my $target (@target) { my $end = $target->{end}; &$end if $end; my $teefh = $target->{teefh}; if ($teefh) { unless (close $teefh) { $error = 1 unless $target->{ignore_errors}; } } } close $out or $error = 1; _exit($error); } # close $teefh; $oldsel = select($fh); no warnings 'once'; ($|, $%, $=, $-, $~, $^, $.) = @oldstate; select($oldsel); return $pid; } 1; __END__ =head1 NAME File::Tee - replicate data sent to a Perl stream =head1 SYNOPSIS use File::Tee qw(tee); # simple usage: tee(STDOUT, '>', 'stdout.txt'); print "hello world\n"; system "ls"; # advanced usage: my $pid = tee STDERR, { prefix => "err[$$]: ", reopen => 'my.log'}; print STDERR "foo\n"; system("cat /bad/path"); =head1 DESCRIPTION This module is able to replicate data written to a Perl stream into another streams. It is the Perl equivalent of the shell utility L. It is implemeted around C, creating a new process for every tee'ed stream. That way, there are no problems handling the output generated by external programs run with L or by XS modules that don't go through L. =head2 API The following function can be imported from this module: =over 4 =item tee $fh, $target, ... redirects a copy of the data written to C<$fh> to one or several files or streams. C<$target, ...> is a list of target streams specifications that can be: =over 4 =item * file names with optional mode specifications: tee STDOUT, '>> /tmp/out', '>> /tmp/out2'; tee STDOUT, '>>', '/tmp/out', '/tmp/out2'; If the mode specification is a separate argument, it will affect all the file names following and not just the nearest one. If mode C<|-> is used as a separate argument, the rest of the arguments are slurped as arguments for the pipe command: tee STDERR, '|-', 'grep', '-i', 'error'; tee STDERR, '| grep -i error'; # equivalent Valid modes are C>, CE>, C&>, CE&> and C<|->. The default mode is CE>. File handles can also be used as targets: open my $target1, '>>', '/foo/bar'; ... tee STDOUT, $target1, $target2, ...; Finally, code references can also be used as targets. The callback will be invoked for every line written to the tee'ed stream with the data in C<$_>. It has to return a true value on success or false if some error happens. Also, note that the callback will be called from a different process. =item * hash references describing the targets For instance: tee STDOUT, { mode => '>>', open => '/tmp/foo', lock => 1}; will copy the data sent to STDOUT to C. The attributes that can be included inside the hash are: =over 4 =item open => $file_name =item reopen => $file_name sets the target file or stream. It can contain a mode specification and also be an array. For instance: tee STDOUT, { open => '>> /tmp/out' }; tee STDOUT, { reopen => ['>>', '/tmp/out2'] }; tee STDOUT, { open => '| grep foo > /tmp/out' }; If C is used, the file or stream is reopen for every write operation. The mode will be forced to append after the first write. =item mode => $mode Alternative way to specify the mode to open the target file or stream =item lock => $bool When true, an exclusive lock is obtained on the target file before writing to it. =item prefix => $txt Some text to be prepended to every line sent to the target file. For instance: tee STDOUT, { prefix => 'OUT: ', lock => 1, mode => '>>', open => '/tmp/out.txt' }; tee STDERR, { prefix => 'ERR: ', lock => 1, mode => '>>', open => '/tmp/out.txt' }; =item preprocess => sub { ... } A callback function that can modify the data before it gets sent to the target file. For instance: sub hexdump { my $data = shift; my @out; while ($data =~ /(.{1,32})/smg) { my $line=$1; my @c= (( map { sprintf "%02x",$_ } unpack('C*', $line)), ((" ") x 32))[0..31]; $line=~s/(.)/ my $c=$1; unpack("c",$c)>=32 ? $c : '.' /egms; push @out, join(" ", @c, '|', $line), "\n"; } join('', @out); } tee BINFH, { preprocess => \&hexdump, open => '/tmp/hexout'}; =item autoflush => $bool Sets autoflush mode for the target streams. Default is on. =item ignore_errors => $bool By default, when writting to the targets, any error will close the tee'ed handle. This option allows to change that behaviour. =item process => sub { ... } the callback will be called for every line read (see using code references as targets discussion above). This option can not be used at the same time as most other options (open, reopen, lock, autoflush, etc.). =item begin => sub { ... } =item end => sub { ... } Those functions are called on the forked process before the first write and when closing the handle respectively. For instance: my @capture; tee STDERR, { process => sub { push @capture, $_ }, end => sub { send_mail 'foo@bar.com', 'stderr capture', "@capture" } }; =back =back The funcion returns the PID for the newly created process. Inside the C pipe process created, data is readed honouring the input record separator C<$/>. You could also want to set the tee'ed stream in autoflush mode: open $fh, ...; my $oldsel = select $fh; $| = 1; select $fh; tee $fh, "> /tmp/log"; =back =head1 BUGS Does not work on Windows (patches welcome). Send bug reports by email or via L. =head1 SEE ALSO L L is a similar module implemented around tied file handles. L allows to launch external processes capturing their output to some files. L allows to capture the output generated from a child process or a subroutine. =head1 COPYRIGHT AND LICENSE Copyright (C) 2007, 2008, 2010, 2011 by Salvador FandiEo (sfandino@yahoo.com) This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. =cut