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