File Coverage

blib/lib/PerlIO/via/Include.pm
Criterion Covered Total %
statement 26 37 70.2
branch 9 14 64.2
condition n/a
subroutine 8 9 88.8
pod 3 5 60.0
total 46 65 70.7


line stmt bran cond sub pod time code
1             package PerlIO::via::Include;
2              
3             $VERSION= '0.05';
4              
5             # be as strict as possible
6 1     1   60892 use strict;
  1         2  
  1         495  
7              
8             # initializations
9             my $before= '^#include ';
10             my $after= $/;
11             my $regexp;
12              
13             # satisfy -require-
14             1;
15              
16             #-------------------------------------------------------------------------------
17             #
18             # Class methods
19             #
20             #-------------------------------------------------------------------------------
21             # IN: 1 class (ignored)
22             # 2 new value for default before string
23             # OUT: 1 current default before string
24              
25             sub before {
26              
27             # set before string
28 2 50   2 1 600 if ( @_ > 1 ) {
29 0         0 $before= $_[1];
30 0         0 $regexp= undef;
31             }
32              
33 2         9 return $before;
34             } #before
35              
36             #-------------------------------------------------------------------------------
37             # IN: 1 class (ignored)
38             # 2 new value for default after string
39             # OUT: 1 current default after string
40              
41             sub after {
42              
43             # set after string
44 2 50   2 1 6 if ( @_ > 1 ) {
45 0         0 $after= $_[1];
46 0         0 $regexp= undef;
47             }
48              
49 2         8 return $after;
50             } #after
51              
52             #-------------------------------------------------------------------------------
53             # IN: 1 class (ignored)
54             # 2 new value for default regular expression string
55             # OUT: 1 current default regular expression string
56              
57             sub regexp {
58              
59             # set new regexp
60 3 100   3 1 10 if ( @_ > 1 ) {
61 1         2 $regexp= $_[1];
62 1         2 $before= $after= undef;
63             }
64              
65 3         15 return $regexp;
66             } #regexp
67              
68             #-------------------------------------------------------------------------------
69             #
70             # Standard Perl features
71             #
72             #-------------------------------------------------------------------------------
73             # IN: 1 class to bless with
74             # 2 mode string (ignored)
75             # 3 file handle of PerlIO layer below (ignored)
76             # OUT: 1 blessed object
77              
78             sub PUSHED {
79              
80             # create the object with the right attributes
81 6 100   6 0 979 return bless {
82             regexp => $regexp ? $regexp : qr/$before(.*?)$after/,
83             } ,$_[0];
84             } #PUSHED
85              
86             #-------------------------------------------------------------------------------
87             # IN: 1 instantiated object
88             # 2 handle to read from
89             # OUT: 1 processed string or undef
90              
91             sub FILL {
92              
93             # process line if there is one
94 26     26 0 50 my $regexp= $_[0]->{'regexp'};
95 26 100       187 if ( defined( my $line= readline( $_[1] ) ) ) {
96 18         72 $line =~ s#$regexp#_include( $1 )#gse;
  4         12  
97 18         85 return $line;
98             }
99              
100 8         47 return undef;
101             } #FILL
102              
103             #-------------------------------------------------------------------------------
104             # IN: 1 instantiated object
105             # 2 buffer to be written
106             # 3 handle to write to
107             # OUT: 1 number of bytes written
108              
109             sub WRITE {
110              
111             # process lines, return if failure
112 0     0   0 my $regexp= $_[0]->{'regexp'};
113 0         0 foreach ( split( m#(?<=$/)#, $_[1] ) ) {
114 0         0 s#$regexp#_include($1)#gse;
  0         0  
115              
116 0 0       0 return -1 if !print { $_[2] } $_;
  0         0  
117             }
118              
119 0         0 return length( $_[1] );
120             } #WRITE
121              
122             #-------------------------------------------------------------------------------
123             # IN: 1 class for which to import
124             # 2..N parameters passed with -use-
125              
126             sub import {
127 2     2   162 my ( $class, %param )= @_;
128              
129             # set using mutators
130 2         9 $class->$_( $param{$_} ) foreach keys %param;
131              
132 2         17 return;
133             } #import
134              
135             #-------------------------------------------------------------------------------
136             #
137             # Internal Subroutines
138             #
139             #-------------------------------------------------------------------------------
140             # IN: 1 filename to open and include
141             # OUT: 1 contents of the whole file
142              
143             sub _include {
144              
145             # huh?
146 4 50   4   48 open( my $handle,"<:via(Include)",$_[0] )
147             or return "*** Could not open '$_[0]': $! ***";
148              
149             # get the contents
150 4         12 my $contents= '';
151 4         14 local($_);
152 4         53 $contents .= $_ while readline( $handle );
153              
154 4         64 return $contents;
155             } #_include
156              
157             #-------------------------------------------------------------------------------
158              
159             __END__