File Coverage

blib/lib/Perl/Tidy/IOScalar.pm
Criterion Covered Total %
statement 28 55 50.9
branch 3 18 16.6
condition 0 8 0.0
subroutine 7 10 70.0
pod 0 3 0.0
total 38 94 40.4


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 39     39   346 use strict;
  39         94  
  39         1330  
11 39     39   265 use warnings;
  39         133  
  39         1326  
12 39     39   233 use Carp;
  39         111  
  39         3500  
13             our $VERSION = '20230909';
14              
15 39     39   268 use constant DEVEL_MODE => 0;
  39         92  
  39         2199  
16 39     39   285 use constant EMPTY_STRING => q{};
  39         138  
  39         25335  
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             }
42              
43       0     sub DESTROY {
44              
45             # required to avoid call to AUTOLOAD in some versions of perl
46             }
47              
48             sub new {
49 553     553 0 2024 my ( $package, $rscalar, $mode ) = @_;
50 553         1420 my $ref = ref $rscalar;
51 553 50       2159 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 553 50       1715 if ( $mode eq 'w' ) {
    0          
60 553         1129 ${$rscalar} = EMPTY_STRING;
  553         1391  
61 553         2891 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             #
68             # NOTES: The -1 count is needed to avoid loss of trailing blank lines
69             # (which might be important in a DATA section).
70 0         0 my @array;
71 0 0 0     0 if ( $rscalar && ${$rscalar} ) {
  0         0  
72              
73             #@array = map { $_ .= "\n" } split /\n/, ${$rscalar}, -1;
74 0         0 @array = map { $_ . "\n" } split /\n/, ${$rscalar}, -1;
  0         0  
  0         0  
75              
76             # remove possible extra blank line introduced with split
77 0 0 0     0 if ( @array && $array[-1] eq "\n" ) { pop @array }
  0         0  
78             }
79 0         0 my $i_next = 0;
80 0         0 return bless [ \@array, $mode, $i_next ], $package;
81             }
82             else {
83 0         0 confess <<EOM;
84             ------------------------------------------------------------------------
85             expecting mode = 'r' or 'w' but got mode ($mode); trace follows:
86             ------------------------------------------------------------------------
87             EOM
88             }
89             }
90              
91             sub getline {
92 0     0 0 0 my $self = shift;
93 0         0 my $mode = $self->[1];
94 0 0       0 if ( $mode ne 'r' ) {
95 0         0 confess <<EOM;
96             ------------------------------------------------------------------------
97             getline call requires mode = 'r' but mode = ($mode); trace follows:
98             ------------------------------------------------------------------------
99             EOM
100             }
101 0         0 my $i = $self->[2]++;
102 0         0 return $self->[0]->[$i];
103             }
104              
105             sub print ## no critic (Subroutines::ProhibitBuiltinHomonyms)
106             {
107 110     110 0 185 my ( $self, $msg ) = @_;
108 110         162 my $mode = $self->[1];
109 110 50       196 if ( $mode ne 'w' ) {
110 0         0 confess <<EOM;
111             ------------------------------------------------------------------------
112             print call requires mode = 'w' but mode = ($mode); trace follows:
113             ------------------------------------------------------------------------
114             EOM
115             }
116 110         149 ${ $self->[0] } .= $msg;
  110         242  
117 110         231 return;
118             }
119             1;