File Coverage

blib/lib/Perl/Tidy/IOScalarArray.pm
Criterion Covered Total %
statement 12 44 27.2
branch 0 14 0.0
condition 0 2 0.0
subroutine 4 9 44.4
pod 0 3 0.0
total 16 72 22.2


line stmt bran cond sub pod time code
1             package Perl::Tidy::IOScalarArray;
2              
3             #####################################################################
4             #
5             # This is a stripped down version of IO::ScalarArray
6             # Given a reference to an array, it supplies either:
7             # a getline method which reads lines (mode='r'), or
8             # a print method which reads lines (mode='w')
9             #
10             # NOTE: this routine assumes that there aren't any embedded
11             # newlines within any of the array elements. There are no checks
12             # for that.
13             #
14             #####################################################################
15              
16 44     44   249 use strict;
  44         76  
  44         1203  
17 44     44   145 use warnings;
  44         87  
  44         1641  
18 44     44   181 use Carp;
  44         60  
  44         2903  
19             our $VERSION = '20260705';
20              
21 44     44   178 use constant DEVEL_MODE => 0;
  44         60  
  44         21871  
22              
23             sub AUTOLOAD {
24              
25             # Catch any undefined sub calls so that we are sure to get
26             # some diagnostic information. This sub should never be called
27             # except for a programming error.
28 0     0     our $AUTOLOAD;
29 0 0         return if ( $AUTOLOAD =~ /\bDESTROY$/ );
30              
31             # Originally there was a dummy sub close. All calls to it should have been
32             # eliminated, but for safety we will check for them here.
33 0 0 0       return 1 if ( $AUTOLOAD =~ /\bclose$/ && !DEVEL_MODE );
34 0           my ( $pkg, $fname, $lno ) = caller();
35 0           my $my_package = __PACKAGE__;
36 0           print {*STDERR} <<EOM;
  0            
37             ======================================================================
38             Error detected in package '$my_package', version $VERSION
39             Received unexpected AUTOLOAD call for sub '$AUTOLOAD'
40             Called from package: '$pkg'
41             Called from File '$fname' at line '$lno'
42             This error is probably due to a recent programming change
43             ======================================================================
44             EOM
45 0           exit 1;
46             } ## end sub AUTOLOAD
47              
48       0     sub DESTROY {
49              
50             # required to avoid call to AUTOLOAD in some versions of perl
51             }
52              
53             sub new {
54 0     0 0   my ( $package, $rarray, $mode ) = @_;
55 0           my $ref = ref($rarray);
56 0 0         if ( $ref ne 'ARRAY' ) {
57 0           confess <<EOM;
58             ------------------------------------------------------------------------
59             expecting ref to ARRAY but got ref to ($ref); trace follows:
60             ------------------------------------------------------------------------
61             EOM
62              
63             }
64 0 0         if ( $mode eq 'w' ) {
    0          
65 0           @{$rarray} = ();
  0            
66 0           return bless [ $rarray, $mode ], $package;
67             }
68             elsif ( $mode eq 'r' ) {
69 0           my $i_next = 0;
70 0           return bless [ $rarray, $mode, $i_next ], $package;
71             }
72             else {
73 0           confess <<EOM;
74             ------------------------------------------------------------------------
75             expecting mode = 'r' or 'w' but got mode ($mode); trace follows:
76             ------------------------------------------------------------------------
77             EOM
78             }
79             } ## end sub new
80              
81             sub getline {
82 0     0 0   my $self = shift;
83 0           my $mode = $self->[1];
84 0 0         if ( $mode ne 'r' ) {
85 0           confess <<EOM;
86             ------------------------------------------------------------------------
87             getline requires mode = 'r' but mode = ($mode); trace follows:
88             ------------------------------------------------------------------------
89             EOM
90             }
91 0           my $i = $self->[2]++;
92 0           return $self->[0]->[$i];
93             } ## end sub getline
94              
95             sub print {
96 0     0 0   my ( $self, $msg ) = @_;
97 0           my $mode = $self->[1];
98 0 0         if ( $mode ne 'w' ) {
99 0           confess <<EOM;
100             ------------------------------------------------------------------------
101             print requires mode = 'w' but mode = ($mode); trace follows:
102             ------------------------------------------------------------------------
103             EOM
104             }
105 0           push @{ $self->[0] }, $msg;
  0            
106 0           return;
107             } ## end sub print
108             1;