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