| 1 | #!/usr/local/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Output::Buffer - module that assists in the capturing of output | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | This module assists in the capture and buffer of data outputted from a program. | ||||||||||||||||||||||||||||
| 10 | For more information please see http://www.theperlreview.com Volume 0 Issue 5 | ||||||||||||||||||||||||||||
| 11 | "Filehandle Ties". | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 TODO | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =over 4 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =item * | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | test.pl | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =back | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 BUGS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | This is a new module and has not been thoroughly tested. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | package Output::Buffer; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # BEHAVIORAL CONSTANTS | ||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use constant WARN => 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use constant FLUSH => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use constant CLEAN => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # EXPORT | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 39 | 1 | @EXPORT_OK = qw(WARN FLUSH CLEAN); | |||||||||||||||||||||||||||
| 40 | 1 | %EXPORT_TAGS = ( constants => [@EXPORT_OK] ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # VERSION | ||||||||||||||||||||||||||||
| 43 | 1 | $VERSION = 0.1; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # DEPENDENCIES | ||||||||||||||||||||||||||||
| 46 | 1 | ▶ | use Tie::FileHandle::Buffer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | use Symbol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Create a new output buffer | ||||||||||||||||||||||||||||
| 52 | # Usage: my $buffer = Output::Buffer->new( behavior ) | ||||||||||||||||||||||||||||
| 53 | # where behavior is either FLUSH, CLEAN, or WARN | ||||||||||||||||||||||||||||
| 54 | # FLUSH - when the object loses scope, print its buffer | ||||||||||||||||||||||||||||
| 55 | # CLEAN - when the object loses scope, discard its buffer | ||||||||||||||||||||||||||||
| 56 | # WARN - when the object loses scope, discard its buffer | ||||||||||||||||||||||||||||
| 57 | # but issue a warning | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 0 | my $fh = gensym; # create an anonymous filehandle | |||||||||||||||||||||||||||
| 60 | 0 | tie *{$fh}, 'Tie::FileHandle::Buffer'; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # store our behavior, our handle, and the handle we replaced | ||||||||||||||||||||||||||||
| 63 | 0 | bless [ $_[1], $fh, select $fh ], $_[0]; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # clean the output buffer, discarding its contents | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub clean { | ||||||||||||||||||||||||||
|
Subroutine
clean: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | 0 | (tied *{$_[0]->[1]})->clear; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # get our contents | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub get_contents { | ||||||||||||||||||||||||||
|
Subroutine
get_contents: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 0 | (tied *{$_[0]->[1]})->get_contents; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # flush the output buffer, printing its contents | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 79 | 0 | my $handle = $self->[2]; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # print our contents to our parent | ||||||||||||||||||||||||||||
| 82 | 0 | print $handle $self->get_contents; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # then discard them | ||||||||||||||||||||||||||||
| 85 | 0 | $self->clean; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # Our scope has ended - deal with it by acting out our behavior | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( $self->[0] == FLUSH ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | # FLUSH means flush! | ||||||||||||||||||||||||||||
| 93 | 0 | $self->flush; | |||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||
| 95 | # only WARN carps - and only if there was buffered output | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | carp "Discarded output buffer contents" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->[0] == 2 and length $self->get_contents != 0
MC/DC0%0/2
$self->[0] == 2 and length $self->get_contents != 0
$self->[0] == 2
length $self->get_contents != 0
Truth table0%0/3
A and B
A= $self->[0] == 2B= length $self->get_contents != 0
| |||||||||||||||||||||||||||||
| 97 | if ( ($self->[0] == WARN) && (length($self->get_contents) != 0)); | ||||||||||||||||||||||||||||
| 98 | # both CLEAN and WARN imply cleaning | ||||||||||||||||||||||||||||
| 99 | 0 | $self->clean; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # return the old filehandle to domination | ||||||||||||||||||||||||||||
| 103 | 0 | my $handle = $self->[2]; | |||||||||||||||||||||||||||
| 104 | 0 | select $handle; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 AUTHORS AND COPYRIGHT | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Written by Robby Walker ( robwalker@cpan.org ) for Point Writer ( http://www.pointwriter.com/ ). | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | You may redistribute/modify/etc. this module under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||