| 1 | #!/usr/bin/perl -w |
| 2 | # vim: ts=2 sw=2 filetype=perl expandtab |
| 3 | |
| 4 | # Make sure that the default behavior for POE::Wheel::FollowTail is to |
| 5 | # skip to the end of the file when it first starts. |
| 6 | |
| 7 | use warningsscip-perl cpan POE 1.370 warnings/; |
| 8 | use strictscip-perl cpan POE 1.370 strict/; |
| 9 | |
| 10 | sub POE::Kernel::ASSERT_DEFAULTscip-perl cpan POE 1.370 main/`POE::Kernel::ASSERT_DEFAULT`(). () { 1 } |
| 11 | |
| 12 | use IO::Handlescip-perl cpan POE 1.370 IO/Handle/; |
| 13 | use POEscip-perl cpan POE 1.370 POE/ qw(Wheel::FollowTail Filter::Line); |
| 14 | use Test::Morescip-perl cpan POE 1.370 Test/More/ tests => 2; |
| 15 | |
| 16 | my $filename = 'bingos-followtail'; |
| 17 | |
| 18 | # Using "!" as a newline to avoid differences in opinion about "\n". |
| 19 | |
| 20 | openscip-perl cpan POE 1.370 main/open(). FH, ">$filename" or diescip-perl cpan POE 1.370 main/die(). "$!\n"; |
| 21 | FH->autoflushscip-perl cpan POE 1.370 FH/autoflush().(1); |
| 22 | print FH "moocow - this line should be skipped!"; |
| 23 | |
| 24 | POE::Session->createscip-perl cpan POE 1.370 POE/Session/create().( |
| 25 | package_states => [ |
| 26 | 'main' => [qw(_start _input _error _shutdown _file_is_idle)], |
| 27 | ], |
| 28 | inline_states => { |
| 29 | _stop => sub { undefscip-perl cpan POE 1.370 main/undef(). }, |
| 30 | }, |
| 31 | heap => { filename => $filename, }, |
| 32 | ); |
| 33 | |
| 34 | $poe_kernelscip-perl cpan POE 1.370 main/$poe_kernel.->runscip-perl cpan POE 1.370 main/run().(); |
| 35 | exitscip-perl cpan POE 1.370 main/exit(). 0; |
| 36 | |
| 37 | sub _startscip-perl cpan POE 1.370 main/_start(). { |
| 38 | my ($kernel,$heap) = @_scip-perl cpan POE 1.370 main/`@_`.[KERNEL,HEAP]; |
| 39 | $heap->{wheel} = POE::Wheel::FollowTail->newscip-perl cpan POE 1.370 POE/Wheel/FollowTail/new().( |
| 40 | Filter => POE::Filter::Line->newscip-perl cpan POE 1.370 POE/Filter/Line/new().( Literal => "!" ), |
| 41 | Filename => $heap->{filename}, |
| 42 | InputEvent => '_input', |
| 43 | ErrorEvent => '_error', |
| 44 | IdleEvent => '_file_is_idle', |
| 45 | ); |
| 46 | |
| 47 | $heap->{running} = 1; |
| 48 | $heap->{counter} = 0; |
| 49 | |
| 50 | print FH "Cows go moo, yes they do!"; |
| 51 | close FH; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | sub _shutdownscip-perl cpan POE 1.370 main/_shutdown(). { |
| 56 | deletescip-perl cpan POE 1.370 main/delete(). $_scip-perl cpan POE 1.370 main/`@_`.[HEAP]->{wheel}; |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | sub _inputscip-perl cpan POE 1.370 main/_input(). { |
| 61 | my ($kernel,$heap,$input) = @_scip-perl cpan POE 1.370 main/`@_`.[KERNEL,HEAP,ARG0]; |
| 62 | |
| 63 | # Make sure we got the right line. |
| 64 | isscip-perl cpan POE 1.370 main/is().($input, 'Cows go moo, yes they do', 'Got the right line'); |
| 65 | okscip-perl cpan POE 1.370 main/ok().( ++$heap->{counter} == 1, 'Cows went moo' ); |
| 66 | POE::Kernel->delayscip-perl cpan POE 1.370 POE/Kernel/delay().( _shutdown => 5 ); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | sub _errorscip-perl cpan POE 1.370 main/_error(). { |
| 71 | my ($heap,$operation, $errnum, $errstr, $wheel_id) = @_scip-perl cpan POE 1.370 main/`@_`.[HEAP,ARG0..ARG3]; |
| 72 | diagscip-perl cpan POE 1.370 main/diag().("Wheel $wheel_id generated $operation error $errnum: $errstr\n"); |
| 73 | POE::Kernel->delayscip-perl cpan POE 1.370 POE/Kernel/delay().( _shutdown => 0.01 ); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | sub _file_is_idlescip-perl cpan POE 1.370 main/_file_is_idle(). { |
| 78 | return unless $_scip-perl cpan POE 1.370 main/`@_`.[HEAP]{counter}; |
| 79 | |
| 80 | # At first I thought just a delay( _shutdown => 1 ) would be nice |
| 81 | # here, but there's a slight chance that the POE::Wheel::FollowTail |
| 82 | # polling interval could refresh this indefinitely. |
| 83 | # |
| 84 | # So I took the slightly more awkward course of turning off the |
| 85 | # shutdown timer and triggering shutdown immediately. |
| 86 | |
| 87 | POE::Kernel->delayscip-perl cpan POE 1.370 POE/Kernel/delay().(_shutdown => undef); |
| 88 | POE::Kernel->yieldscip-perl cpan POE 1.370 POE/Kernel/yield().("_shutdown"); |
| 89 | } |