Take a look at the non-blocked reading mode in [mod://Term::ReadKey]. For example:
#!/usr/bin/perl
use warnings;
use strict;
use Term::ReadKey;
while(1) {
#do some stuff
print "Did something\n";
sleep 5;
if (my $line= ReadLine -1) {
print "Got this command: $line";
}
}
$ perl current_users.pl `pwd`
##############################################
GFMIS Exlink User Activity
Start: 01/02/2006 10:24 : Host = EU1
##############################################
2006-01-25 15:38:24,953: Trader (DBL99911) = Connected
DISPLAY
2006-01-25 15:38:24,953: Trader (DBL99911) = Connected
Hey i got the following signal INT
Somebody sent me a SIGINT at current_users.pl line 110.
Sorry, my magic crystal ball is still out for repair (damn Grima!). You'll need to post code for anyone to figure out what's wrong in your script. Please make an effort to cut it down to the part that's necessary (i.e. the bit that's not doing what you think it ought to) before doing so though.
$file=File::Tail->new(name=>$name, maxinterval=>1, adjustafter=>1);
while (defined($line=$file->read)) {
my($logindate,$loginuser,$logoutdate,$logoutuser,$count);
#if($line =~ /validateLogin\((.*)\)/){ $v = $1;}
if($line =~ /INFO\s\s(\d+)(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+),(\d+)\s\[(.*)\]\s\((.*)\) - validateLogin\((.*)\)/){
$logindate = "$1$2-$3-$4 $5:$6:$7,$8";
$loginuser = "$11";
if(($logging)&&($debug)){&writelog(1,"DEBUG: Tail Event Login found user date string = $logindate user = $loginuser");}
}
}
So i guess somewhere whilst it is processing the file i would want to be able to lookout for user intput on the terminal
Your while loop only executes whenever File::Tail finds a new line appended to the file you're watching. So you could do this (reformatted a bit for readability):
my $file=File::Tail->new( name => $name,
maxinterval => 1,
adjustafter => 1);
while (defined(my $line=$file->read)) {
my($logindate,$loginuser,$logoutdate,$logoutuser,$count);
if($line =~ /INFO\s\s(\d+)(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+),(\d+)\s\[(.*)\]\s\((.*)\) - validateLogin\((.*)\)/){
$logindate = "$1$2-$3-$4 $5:$6:$7,$8";
$loginuser = "$11";
if(($logging)&&($debug)){
&writelog(1,"DEBUG: Tail Event Login found user date string = $logindate user = $loginuser");
}
}
if (my $inp= ReadLine (-1)) {
print "Got this command: $inp";
}
}
And this would work, but the loop would only check for input anytime a line is added to the log, and block until then. If you don't want to do that you'll need to use File::Tail->select instead of read, take a look at the example that comes with File::Tail and the perldoc on that.
#!/usr/bin/perl
use warnings;
use strict;
use IO::Select;
# by edan of perlmonks
# I just remembered what select says about mixing
# buffered reading and "select", so even though the
# code works, you might want to substitute
# the read via <$fh> with:
# my $input;
# sysread( $fh, $input, 1024);
# loop every 1 second
my $timeout = 1;
my $s = IO::Select->new();
$s->add( \*STDIN );
while (1) {
if ( my @ready = $s->can_read($timeout) ) {
# we got input
for my $fh (@ready) {
my $input = <$fh>;
print "got: $input";
}
}
else {
# no input
}
# just to show that we're looping
print scalar localtime,"\n";
}
perlmonks.org content © perlmonks.org and blazar, minixman, tirwhan, zentara
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03