File Coverage

blib/lib/Output/Buffer.pm
Criterion Covered Total %
statement 24 43 55.8
branch 0 4 0.0
condition 0 3 0.0
subroutine 8 13 61.5
pod 0 4 0.0
total 32 67 47.7


line stmt bran cond sub pod time code
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     1   17827 use constant WARN => 2;
  1         3  
  1         106  
33 1     1   6 use constant FLUSH => 1;
  1         3  
  1         49  
34 1     1   5 use constant CLEAN => 0;
  1         22  
  1         53  
35            
36             # EXPORT
37 1     1   5 use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION);
  1         2  
  1         152  
38             @ISA = qw(Exporter);
39             @EXPORT_OK = qw(WARN FLUSH CLEAN);
40             %EXPORT_TAGS = ( constants => [@EXPORT_OK] );
41            
42             # VERSION
43             $VERSION = 0.1;
44            
45             # DEPENDENCIES
46 1     1   1078 use Tie::FileHandle::Buffer;
  1         2188  
  1         29  
47 1     1   3702 use Symbol;
  1         1131  
  1         150  
48 1     1   9 use Carp;
  1         2  
  1         70  
49 1     1   6 use strict;
  1         1  
  1         295  
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             sub new {
59 0     0 0   my $fh = gensym; # create an anonymous filehandle
60 0           tie *{$fh}, 'Tie::FileHandle::Buffer';
  0            
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             sub clean {
68 0     0 0   (tied *{$_[0]->[1]})->clear;
  0            
69             }
70            
71             # get our contents
72             sub get_contents {
73 0     0 0   (tied *{$_[0]->[1]})->get_contents;
  0            
74             }
75            
76             # flush the output buffer, printing its contents
77             sub flush {
78 0     0 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             sub DESTROY {
90 0     0     my $self = shift;
91 0 0         if ( $self->[0] == FLUSH ) {
92             # FLUSH means flush!
93 0           $self->flush;
94             } else {
95             # only WARN carps - and only if there was buffered output
96 0 0 0       carp "Discarded output buffer contents"
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;
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