File Coverage

blib/lib/Perl/Tidy/IOScalar.pm
Criterion Covered Total %
statement 28 52 53.8
branch 3 16 18.7
condition 0 5 0.0
subroutine 7 10 70.0
pod 0 3 0.0
total 38 86 44.1


line stmt bran cond sub pod time code
1             #####################################################################
2             #
3             # This is a stripped down version of IO::Scalar
4             # Given a reference to a scalar, it supplies either:
5             # a getline method which reads lines (mode='r'), or
6             # a print method which reads lines (mode='w')
7             #
8             #####################################################################
9             package Perl::Tidy::IOScalar;
10 44     44   242 use strict;
  44         82  
  44         1410  
11 44     44   174 use warnings;
  44         68  
  44         2189  
12 44     44   180 use Carp;
  44         70  
  44         3166  
13             our $VERSION = '20260204';
14              
15 44     44   206 use constant DEVEL_MODE => 0;
  44         70  
  44         2798  
16 44     44   219 use constant EMPTY_STRING => q{};
  44         76  
  44         23235  
17              
18             sub AUTOLOAD {
19              
20             # Catch any undefined sub calls so that we are sure to get
21             # some diagnostic information. This sub should never be called
22             # except for a programming error.
23 0     0   0 our $AUTOLOAD;
24 0 0       0 return if ( $AUTOLOAD =~ /\bDESTROY$/ );
25              
26             # Originally there was a dummy sub close. All calls to it should have been
27             # eliminated, but for safety we will check for them here.
28 0 0 0     0 return 1 if ( $AUTOLOAD =~ /\bclose$/ && !DEVEL_MODE );
29 0         0 my ( $pkg, $fname, $lno ) = caller();
30 0         0 my $my_package = __PACKAGE__;
31 0         0 print {*STDERR} <<EOM;
  0         0  
32             ======================================================================
33             Error detected in package '$my_package', version $VERSION
34             Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
35             Called from package: '$pkg'
36             Called from File '$fname' at line '$lno'
37             This error is probably due to a recent programming change
38             ======================================================================
39             EOM
40 0         0 exit 1;
41             } ## end sub AUTOLOAD
42              
43       0     sub DESTROY {
44              
45             # required to avoid call to AUTOLOAD in some versions of perl
46             }
47              
48             sub new {
49 640     640 0 1828 my ( $package, $rscalar, $mode ) = @_;
50 640         1488 my $ref = ref($rscalar);
51 640 50       2135 if ( $ref ne 'SCALAR' ) {
52 0         0 confess <<EOM;
53             ------------------------------------------------------------------------
54             expecting ref to SCALAR but got ref to ($ref); trace follows:
55             ------------------------------------------------------------------------
56             EOM
57              
58             }
59 640 50       1872 if ( $mode eq 'w' ) {
    0          
60 640         1127 ${$rscalar} = EMPTY_STRING;
  640         1663  
61 640         3030 return bless [ $rscalar, $mode ], $package;
62             }
63             elsif ( $mode eq 'r' ) {
64              
65             # Convert a scalar to an array.
66             # This avoids looking for "\n" on each call to getline
67 0         0 my @array;
68 0 0 0     0 if ( $rscalar && ${$rscalar} ) {
  0         0  
69 0         0 @array = split /^/, ${$rscalar};
  0         0  
70             }
71 0         0 my $i_next = 0;
72 0         0 return bless [ \@array, $mode, $i_next ], $package;
73             }
74             else {
75 0         0 confess <<EOM;
76             ------------------------------------------------------------------------
77             expecting mode = 'r' or 'w' but got mode ($mode); trace follows:
78             ------------------------------------------------------------------------
79             EOM
80             }
81             } ## end sub new
82              
83             sub getline {
84 0     0 0 0 my $self = shift;
85 0         0 my $mode = $self->[1];
86 0 0       0 if ( $mode ne 'r' ) {
87 0         0 confess <<EOM;
88             ------------------------------------------------------------------------
89             getline call requires mode = 'r' but mode = ($mode); trace follows:
90             ------------------------------------------------------------------------
91             EOM
92             }
93 0         0 my $i = $self->[2]++;
94 0         0 return $self->[0]->[$i];
95             } ## end sub getline
96              
97             sub print {
98 94     94 0 104 my ( $self, $msg ) = @_;
99 94         97 my $mode = $self->[1];
100 94 50       116 if ( $mode ne 'w' ) {
101 0         0 confess <<EOM;
102             ------------------------------------------------------------------------
103             print call requires mode = 'w' but mode = ($mode); trace follows:
104             ------------------------------------------------------------------------
105             EOM
106             }
107 94         83 ${ $self->[0] } .= $msg;
  94         126  
108 94         117 return;
109             } ## end sub print
110             1;