Index

perl modules

general module information
cpan -D [module]
install module
cpan [module]...
view documentation of module
perldoc [module]

list of useful modules

exception handling (try, catch, finally)
Try::Tiny
lists
List::Util
List::MoreUtils
interacting with the native command line
Capture::Tiny
IPC::Open3
IPC::Pipeline
IPC::Run
IPC::System::Simple
nonblocking IO
IO::Select
tailing files
File::Tail

File::Tail

basic tail implementation
#!/usr/bin/perl
use 5.26.1;
use File::Tail; 
 
my $timeout = 0.1;
my @files = (); 
push @files, File::Tail->new(name=>"$_") for (@ARGV); 
my $count = @files;
 
while ($count > 0) {
        my ($found, $timeleft, @pending) = File::Tail::select(undef, undef, undef, $timeout, @files);
        if ($found) { 
                print "file:" . $_->{"input"} . " line:" . $_->read for (@pending);
        } 
}

IO::Select

non-blocking read of <STDIN>
#!/usr/bin/perl
use 5.26.1;
use IO::Select;

my $timeout = 0.1;
my $stdin = IO::Select->new();
$stdin->add(\*STDIN);
  
while (1) {
        if($stdin->can_read($timeout)) {
                my $line = <STDIN>;
                print ">>>>>>>> $line";
        } else { 
                # todo: code goes here
        } 
}
non-blocking read from both system input and file
#!/usr/bin/perl
use 5.26.1;
use IO::Select;
use File::Tail;
no strict "vars";

$timeout = 0.1;
$stdin = IO::Select->new();
$stdin->add(\*STDIN);
@files = (File::Tail->new(name=>"input.txt"));

while (1) {
        if($stdin->can_read($timeout)) {
                $line = <STDIN>;
                print "<STDIN> $line";
        } else {
                ($found, $timeleft, @pending) = File::Tail::select(undef,undef,undef,$timeout,@files);
                if ($found) {
	                print "file:" . $_->{"input"} . " line:" . $_->read for (@pending);
                }
        }
}