#!/usr/bin/env perl use strict; use warnings; use Time::HiRes qw(usleep); use IO::Select; use File::Spec::Functions qw(catfile); use constant USLEEP_TIME => 25_000; # Microseconds use constant RUN_TIME => 30; # Seconds my $cmd = catfile((getpwnam($ENV{USER}))[7],'scripts','outputter'); # This just sets up the path to the external script. print "Our command is >>>$cmd<<<\n"; open my $r, '-|', $cmd or die $!; # Open a pipe to read output from a command. my $s = IO::Select->new($r); # Create an IO::Select object on our pipe's filehandle, $r. my $pinged = 0; # Poor-man's messaging: We haven't received a ping yet. my $start = time(); # Record our start time so we can calculate when to exit. while(time() < $start + RUN_TIME) { # Iterate until we run out of time. if ($r->eof) { # Finish if the target command has finished all output and termianted. last; } elsif ($s->can_read(0)) { # See if there's something available to read. chomp(my $i = <$r>); # Read from our pipe and chomp. print "\n<$i>\n"; # Print what we read. $pinged = 1 if $i eq 'ping'; # Send a message that we read something. } elsif ($pinged) { # If we have a ping message, print a pong. print "(pong)\n"; $pinged = 0; # And unset the message. } else { usleep USLEEP_TIME; # If there's nothing to do, sleep briefly. print '.'; # Print something to let everyone know we're thinking of them. STDOUT->flush; } } # lather, rinse, repeat.