This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move EU::ParseXS::CountLines to its own file to simplify editing
[perl5.git] / dist / ExtUtils-ParseXS / lib / ExtUtils / ParseXS / CountLines.pm
CommitLineData
9e831c8e
JK
1package ExtUtils::ParseXS::CountLines;
2use strict;
3our $SECTION_END_MARKER;
4
5sub TIEHANDLE {
6 my ($class, $cfile, $fh) = @_;
7 $cfile =~ s/\\/\\\\/g;
8 $SECTION_END_MARKER = qq{#line --- "$cfile"};
9
10 return bless {
11 buffer => '',
12 fh => $fh,
13 line_no => 1,
14 }, $class;
15}
16
17sub PRINT {
18 my $self = shift;
19 for (@_) {
20 $self->{buffer} .= $_;
21 while ($self->{buffer} =~ s/^([^\n]*\n)//) {
22 my $line = $1;
23 ++$self->{line_no};
24 $line =~ s|^\#line\s+---(?=\s)|#line $self->{line_no}|;
25 print {$self->{fh}} $line;
26 }
27 }
28}
29
30sub PRINTF {
31 my $self = shift;
32 my $fmt = shift;
33 $self->PRINT(sprintf($fmt, @_));
34}
35
36sub DESTROY {
37 # Not necessary if we're careful to end with a "\n"
38 my $self = shift;
39 print {$self->{fh}} $self->{buffer};
40}
41
42sub UNTIE {
43 # This sub does nothing, but is neccessary for references to be released.
44}
45
46sub end_marker {
47 return $SECTION_END_MARKER;
48}
49
501;