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