File: | t/lib/Tie/Input/Insertable.pm |
Coverage: | 43.3% |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | # Excellent snippet of code proferred by (Archbishop) "tilly" in response | ||||||
2 | # to someone asking how to provide fake input on STDIN. | ||||||
3 | |||||||
4 | package Tie::Input::Insertable; | ||||||
5 | |||||||
6 | sub PRINT | ||||||
7 | { | ||||||
8 | 1 | 1 | my $self = shift; | ||||
9 | 1 | 8 | $self->{buffer} = join '', @_, $self->{buffer}; | ||||
10 | } | ||||||
11 | |||||||
12 | sub TIEHANDLE | ||||||
13 | { | ||||||
14 | 1 | 4 | my ( $class, $fh ) = @_; | ||||
15 | 1 | 6 | bless { fh => $fh, buffer => "" }, $class; | ||||
16 | } | ||||||
17 | |||||||
18 | sub READLINE | ||||||
19 | { | ||||||
20 | 1 | 1 | my $self = shift; | ||||
21 | 1 | 4 | return undef if $self->{eof}; | ||||
22 | 1 | 5 | while ( -1 == index( $self->{buffer}, $/ ) ) { | ||||
23 | 0 | 0 | my $fh = $self->{fh}; | ||||
24 | 0 | 0 | my $data = <$fh>; | ||||
25 | 0 | 0 | if ( length( $data ) ) { | ||||
26 | 0 | 0 | $self->{buffer} .= $data; | ||||
27 | } | ||||||
28 | else { | ||||||
29 | 0 | 0 | $self->{eof} = 1; | ||||
30 | 0 | 0 | return delete $self->{buffer}; | ||||
31 | } | ||||||
32 | } | ||||||
33 | 1 | 2 | my $pos = index( $self->{buffer}, $/ ) + length( $/ ); | ||||
34 | 1 | 4 | return substr( $self->{buffer}, 0, $pos, "" ); | ||||
35 | } | ||||||
36 | |||||||
37 | sub EOF | ||||||
38 | { | ||||||
39 | 0 | my $self = shift; | |||||
40 | 0 | $self->{eof} ||= not length( $self->{buffer} ) or $self->{fh}->eof(); | |||||
41 | } | ||||||
42 | |||||||
43 | #tie *FAKEIN, 'Tie::Input::Insertable', *STDIN; | ||||||
44 | # | ||||||
45 | #while (<FAKEIN>) { | ||||||
46 | # print FAKEIN "Hello, world\n" if /foo/; | ||||||
47 | # print $_; | ||||||
48 | #} | ||||||
49 | |||||||
50 | 1; |