| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# $Id: /mirror/perl/Iterator-File-Line/trunk/lib/Iterator/File/Line.pm 42691 2008-02-25T01:07:47.310615Z daisuke $ |
|
2
|
|
|
|
|
|
|
# |
|
3
|
|
|
|
|
|
|
# Copyright (c) 2008 Daisuke Maki |
|
4
|
|
|
|
|
|
|
# All rights reserved. |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Iterator::File::Line; |
|
7
|
3
|
|
|
3
|
|
196976
|
use strict; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
141
|
|
|
8
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
|
3
|
|
|
|
|
5
|
|
|
|
3
|
|
|
|
|
109
|
|
|
9
|
3
|
|
|
3
|
|
28
|
use base qw(Class::Accessor::Fast); |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
3249
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.00002'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors($_) for qw(source filter chomp encoding eol); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new |
|
16
|
|
|
|
|
|
|
{ |
|
17
|
5
|
|
|
5
|
1
|
17750
|
my $class = shift; |
|
18
|
5
|
|
|
|
|
27
|
my %args = @_; |
|
19
|
|
|
|
|
|
|
|
|
20
|
5
|
50
|
|
|
|
37
|
if ( $args{filename} ) { |
|
|
|
50
|
|
|
|
|
|
|
21
|
0
|
|
|
|
|
0
|
return $class->new_from_file( %args ); |
|
22
|
|
|
|
|
|
|
} elsif ($args{fh}) { |
|
23
|
5
|
|
|
|
|
31
|
return $class->new_from_fh( %args ); |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
0
|
|
|
|
|
0
|
die "Must provide filename or file handle"; |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub new_from_file |
|
30
|
|
|
|
|
|
|
{ |
|
31
|
0
|
|
|
0
|
1
|
0
|
my $class = shift; |
|
32
|
0
|
|
|
|
|
0
|
my %args = @_; |
|
33
|
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
0
|
my $filename = delete $args{filename}; |
|
35
|
0
|
0
|
|
|
|
0
|
open(my $fh, '<', $filename) or die "Could not open $filename"; |
|
36
|
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
$class->new_from_fh( %args, fh => $fh ); |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub new_from_fh |
|
41
|
|
|
|
|
|
|
{ |
|
42
|
5
|
|
|
5
|
1
|
11
|
my $class = shift; |
|
43
|
5
|
|
|
|
|
18
|
my %args = @_; |
|
44
|
5
|
|
|
|
|
13
|
my $fh = delete $args{fh}; |
|
45
|
|
|
|
|
|
|
|
|
46
|
5
|
100
|
|
|
|
76
|
my $self = $class->SUPER::new( { |
|
47
|
|
|
|
|
|
|
filter => $args{filter}, |
|
48
|
|
|
|
|
|
|
chomp => exists $args{chomp} ? $args{chomp} : 1, |
|
49
|
|
|
|
|
|
|
eol => $args{eol}, |
|
50
|
|
|
|
|
|
|
encoding => $args{encoding}, |
|
51
|
|
|
|
|
|
|
source => $fh, |
|
52
|
|
|
|
|
|
|
} ); |
|
53
|
|
|
|
|
|
|
|
|
54
|
5
|
|
|
|
|
76
|
$self->_setup_layers(); |
|
55
|
5
|
|
|
|
|
3293
|
return $self; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub next |
|
59
|
|
|
|
|
|
|
{ |
|
60
|
13
|
|
|
13
|
1
|
4635
|
my $self = shift; |
|
61
|
13
|
|
|
|
|
43
|
my $fh = $self->source; |
|
62
|
13
|
|
|
|
|
132
|
my $line = <$fh>; |
|
63
|
13
|
100
|
|
|
|
41
|
return undef unless defined $line; |
|
64
|
12
|
100
|
|
|
|
38
|
chomp $line if $self->chomp; |
|
65
|
12
|
100
|
|
|
|
86
|
return $self->filter ? $self->filter->( $line ) : $line; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub rewind |
|
69
|
|
|
|
|
|
|
{ |
|
70
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
|
71
|
1
|
|
|
|
|
4
|
my $fh = $self->source; |
|
72
|
1
|
|
|
|
|
7
|
seek($fh, 0, 0); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _setup_layers |
|
76
|
|
|
|
|
|
|
{ |
|
77
|
5
|
|
|
5
|
|
147
|
my $self = shift; |
|
78
|
5
|
|
|
|
|
21
|
my $fh = $self->source; |
|
79
|
|
|
|
|
|
|
|
|
80
|
5
|
|
|
|
|
48
|
my @layers = ('', 'raw'); |
|
81
|
5
|
100
|
|
|
|
17
|
if ($self->eol) { |
|
82
|
1
|
|
|
|
|
8
|
push @layers, sprintf('eol(%s)', $self->eol ); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
5
|
100
|
|
|
|
40
|
if ($self->encoding) { |
|
86
|
1
|
|
|
|
|
11
|
push @layers, sprintf('encoding(%s)', $self->encoding); |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
5
|
|
|
1
|
|
110
|
binmode($fh, join(':', @layers)); |
|
|
1
|
|
|
1
|
|
7
|
|
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
13
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
11
|
|
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
__END__ |