1 #####################################################################
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')
8 #####################################################################
9 package Perl::Tidy::IOScalar;
13 our $VERSION = '20181120';
16 my ( $package, $rscalar, $mode ) = @_;
17 my $ref = ref $rscalar;
18 if ( $ref ne 'SCALAR' ) {
20 ------------------------------------------------------------------------
21 expecting ref to SCALAR but got ref to ($ref); trace follows:
22 ------------------------------------------------------------------------
28 return bless [ $rscalar, $mode ], $package;
30 elsif ( $mode eq 'r' ) {
32 # Convert a scalar to an array.
33 # This avoids looking for "\n" on each call to getline
35 # NOTES: The -1 count is needed to avoid loss of trailing blank lines
36 # (which might be important in a DATA section).
38 if ( $rscalar && ${$rscalar} ) {
40 #@array = map { $_ .= "\n" } split /\n/, ${$rscalar}, -1;
41 @array = map { $_ . "\n" } split /\n/, ${$rscalar}, -1;
43 # remove possible extra blank line introduced with split
44 if ( @array && $array[-1] eq "\n" ) { pop @array }
47 return bless [ \@array, $mode, $i_next ], $package;
51 ------------------------------------------------------------------------
52 expecting mode = 'r' or 'w' but got mode ($mode); trace follows:
53 ------------------------------------------------------------------------
60 my $mode = $self->[1];
63 ------------------------------------------------------------------------
64 getline call requires mode = 'r' but mode = ($mode); trace follows:
65 ------------------------------------------------------------------------
69 return $self->[0]->[$i];
73 my ( $self, $msg ) = @_;
74 my $mode = $self->[1];
77 ------------------------------------------------------------------------
78 print call requires mode = 'w' but mode = ($mode); trace follows:
79 ------------------------------------------------------------------------
82 ${ $self->[0] } .= $msg;