POE::W...owTail(3pm)
π POE::Wheel::FollowTail π
okay bestie, so this is the manual page for POE::Wheel::FollowTail, straight out of the User Contributed Perl Documentation section, and it's literally about following the tail of an ever-growing file. like tail -f but make it β¨event-drivenβ¨. let's go π¦
πΈ NAME πΈ
POE::Wheel::FollowTail β follow the tail of an ever-growing file π
β¨ SYNOPSIS β¨
#!perl
use POE qw(Wheel::FollowTail);
POE::Session->create(
inline_states => {
_start => sub {
$_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
Filename => "/var/log/system.log",
InputEvent => "got_log_line",
ResetEvent => "got_log_rollover",
);
},
got_log_line => sub {
print "Log: $_[ARG0]\n";
},
got_log_rollover => sub {
print "Log rolled over.\n";
},
}
);
POE::Kernel->run();
exit;
no cap, that's the whole vibe right there π
π«Ά DESCRIPTION π«Ά
POE::Wheel::FollowTail objects watch for new data at the end of a file and generate new events when things happen to the file. its Filter parameter defines how to parse data from the file. each new item gets sent to the creator's session as an InputEvent event. log rotation will trigger a ResetEvent. it's giving very attentive bestie energy honestly π
one thing to know: POE::Wheel::FollowTail only reads from a file, so it doesn't implement a put() method. it's a read-only queen, periodt. π
π PUBLIC METHODS π
π« new π«
new() returns a new POE::Wheel::FollowTail object. as long as this object exists, it will generate events when the corresponding file's status changes. iconic behavior fr fr.
new() accepts a small set of named parameters:
Driverβ this optional parameter says which driver to use when reading from the tailed file. if you omit it,POE::Wheel::FollowTailwill usePOE::Driver::SysRW. and ngl, that's almost always the right thing to do. don't overthink it bestie πFilterβ an optional constructor parameter that specifies how to parse data from the followed file. by default,POE::Wheel::FollowTailwill usePOE::Filter::Lineto parse files as plain, newline-separated text. and if you wanna swap it up:
$_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
Filename => "/var/log/snort/alert",
Filter => POE::Filter::Snort->new(),
InputEvent => "got_snort_alert",
);
PollIntervalβPOE::Wheel::FollowTailneeds to periodically check for new data on the followed file, andPollIntervalspecifies the number of seconds to wait between checks. applications that need to poll once per second may omitPollInterval, because it defaults to 1. longer poll intervals may be used to reduce the polling overhead for infrequently updated files. lowkey a whole optimization moment πΏ
$_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
...,
PollInterval => 10,
);
Seekβ if specified,SeekinstructsPOE::Wheel::FollowTailto seek to a specific spot in the tailed file before beginning to read from it. a positiveSeekvalue is interpreted as the number of octets to seek from the start of the file. negativeSeekwill, like negative array indices, seek backwards from the end of the file. zeroSeekstarts reading from the beginning of the file. π
β οΈ be careful when usingSeek, because it's quite easy to seek into the middle of a record. when in doubt, and when beginning at the end of the file, omitSeekentirely.POE::Wheel::FollowTailwill seek 4 kilobytes back from the end of the file, then parse and discard all records unto EOF. as long as the file's records are smaller than 4 kilobytes, this will guarantee that the first record returned will be complete. that's the magic trick, iykyk πͺ
Seek may also be used with the wheel's tell() method to restore the file position after a program restart. save the tell() value prior to exiting, and load and Seek back to it on subsequent start-up. chef's kiss π§π³π
SeekBackβSeekBackbehaves like the inverse ofSeek. a positive value acts like a negativeSeek. a negative value acts like a positiveSeek. a zeroSeekBackinstructsPOE::Wheel::FollowTailto begin at the very end of the file. π
SeekandSeekBackare mutually exclusive.
see Seek for caveats, techniques, and an explanation of the magic that happens when neither Seek nor SeekBack is specified. β¨
HandleβPOE::Wheel::FollowTailmay follow a previously opened fileHandle. unfortunately it cannot follow log resets this way, as it won't be able to reopen the file once it has been reset. applications that must follow resets should useFilenameinstead. π
Handle is still useful for files that will never be reset, or for devices that require setup outside of POE::Wheel::FollowTail's purview.
HandleandFilenameare mutually exclusive. one of them is required, however. π
Filenameβ specify theFilenameto watch.POE::Wheel::FollowTailwill wait for the file to appear if it doesn't exist. the wheel will also reopen the file if it disappears, such as when it has been reset or rolled over. in the case of a reset,POE::Wheel::FollowTailwill also emit aResetEvent, if one has been requested. πΈ
HandleandFilenameare mutually exclusive. one of them is required, however.
see the SYNOPSIS for an example. it's right up there bestie β¬οΈ
IdleEventβ an optional event. if specified, it will fire wheneverPOE::Wheel::FollowTailchecks for activity but sees nothing. it was added in POE 1.362 as a way to advance certain test programs without needing to wait conservatively large amounts of time.IdleEventis described inPUBLIC EVENTS. π°οΈInputEventβ this parameter is required, and it specifies the event to emit when new data arrives in the watched file.InputEventis described in detail inPUBLIC EVENTS. π₯ResetEventβ optional. it specifies the name of the event that indicates file rollover or reset. please seePUBLIC EVENTSfor more details. πErrorEventβPOE::Wheel::FollowTailmay emit optionalErrorEvents whenever it runs into trouble. the data that comes with this event is explained inPUBLIC EVENTS. π
π¦ event π¦
event() allows a session to change the events emitted by a wheel without destroying and re-creating the object. it accepts one or more of the events listed in PUBLIC EVENTS. undefined event names disable those events.
stop handling log resets, just like that:
sub some_event_handler {
$_[HEAP]{tailor}->event( ResetEvent => undef );
}
the events are described in more detail in PUBLIC EVENTS. π
π ID π
the ID() method returns the wheel's unique ID. it's useful for storing the wheel in a hash. all POE::Wheel events should be accompanied by a wheel ID, which allows the wheel to be referenced in their event handlers. main character identification moment β¨
sub setup_tailor {
my $wheel = POE::Wheel::FollowTail->new(... incomplete ...);
$_[HEAP]{tailors}{$wheel->ID} = $wheel;
}
see the example in ErrorEvent for a handler that will find this wheel again. π
π tell π
tell() returns the current position for the file being watched by POE::Wheel::FollowTail. it may be useful for saving the position program termination. new()'s Seek parameter may be used to resume watching the file where tell() left off. πΎ
sub handle_shutdown {
# Not robust. Do better in production.
open my $save, ">", "position.save" or die $!;
print $save $_[HEAP]{tailor}->tell(), "\n";
close $save;
}
sub handle_startup {
open my $save, "<", "position.save" or die $!;
chomp(my $seek = <$save>);
$_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
...,
Seek => $seek,
);
}
π that comment in there saying # Not robust. Do better in production. is literally in the original, so yeah, do better in production bestie.
π PUBLIC EVENTS π
POE::Wheel::FollowTail emits a small number of events. small but mighty π«Ά
π IdleEvent π
IdleEvent specifies the name of an event to be fired when POE::Wheel::FollowTail doesn't detect activity on the watched file.
$_[ARG0] contains the ID of the POE::Wheel::FollowTail object that fired the event.
π InputEvent π
InputEvent sets the name of the event to emit when new data arrives into the tailed file. the event will be accompanied by two parameters:
$_[ARG0]contains the data that was read from the file, after being parsed by the currentFilter. πΈ$_[ARG1]contains the wheel's ID, which may be used as a key into a data structure tracking multiple wheels. no assumption should be made about the nature or format of this ID, as it may change at any time. therefore, track your wheels in a hash. fr fr, put them in a hash ποΈ
see the SYNOPSIS for an example.
π ResetEvent π
ResetEvent names the event to be emitted whenever the wheel detects that the followed file has been reset. it's only available when watching files by name, as POE::Wheel::FollowTail must reopen the file after it has been reset.
ResetEvent comes with only one parameter, $_[ARG0], which contains the wheel's ID. see InputEvent for some notes about what may be done with wheel IDs.
see the SYNOPSIS for an example.
π ErrorEvent π
ErrorEvent names the event emitted when POE::Wheel::FollowTail encounters a problem. every ErrorEvent comes with four parameters that describe the error and its situation:
$_[ARG0]describes the operation that failed. this is usuallyread, sincePOE::Wheel::FollowTailspends most of its time reading from a file. π$_[ARG1]and$_[ARG2]contain the numeric and stringified values of$!, respectively. they will never containEAGAIN(or its local equivalent) sincePOE::Wheel::FollowTailhandles that error itself. πͺ$_[ARG3]contains the wheel's ID, which has been discussed inInputEvent.
this error handler logs a message to STDERR and then shuts down the wheel. it assumes that the session is watching multiple files.
sub handle_tail_error {
my ($operation, $errnum, $errstr, $wheel_id) = @_[ARG0..ARG3];
warn "Wheel $wheel_id: $operation error $errnum: $errstr\n";
delete $_[HEAP]{tailors}{$wheel_id};
}
π SEE ALSO π
POE::Wheel describes the basic operations of all wheels in more depth. you need to know this. like, actually. go read it. π
the SEE ALSO section in POE contains a table of contents covering the entire POE distribution. whole menu of options, slay π½οΈ
π BUGS π
this wheel can't tail pipes and consoles on some operating systems. sad but true π
POE::Wheel::FollowTail generally reads ahead of the data it returns, so the tell() position may be later in the file than the data an application has already received. just so you know, so you're not surprised π
π AUTHORS & COPYRIGHTS π
please see POE for more information about authors and contributors. π
and that's the whole page bestie, no notes, you ate π β¨
perl v5.34.0 Β· 2022-03-25 Β· mwah, go tail some logs π¦π
π¦ where this page comes from
- debian bookworm: libpoe-perl 2:1.3700-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libpoe-perl 2:1.3700-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libpoe-perl 2:1.3700-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libpoe-perl 2:1.3700-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms