Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process watcher?

Status
Not open for further replies.

DANZIG

Technical User
Mar 8, 2001
142
US
Hello All, I'm trying to write a script using TK and ran in to a problem.

I need to have the script running and watching a <FILE HANDLE> at the same time. The window is built useing a 'TEXT' widget. I'm wanting to have a file watched and when a NEW line appears in the <FILE> to have the new $_ displayed in the text box.

Any suggestions and or examples would be GREATLY appreciated. Below is what I'm working with now.

use Tk;
$input = text_in;
open INPUT, $input or die &quot;Cannot open $input for read :$!&quot;;

my $mw = MainWindow->new();


$mw->Label(-text => 'Members Listing:')->pack();
my $listbox = $mw->Scrolled('Listbox',
-width => 35,
-height => 5,
-scrollbars => 'e')->pack();

$listbox->insert('end', 'John Coltrane', 'Coleman Hawkins',
'Dexter Gordon', 'Charlie Parker', 'Lester Young',
'Ben Webster', 'Charlie Parker', 'Lester Young');


$mw->Label(-text => 'Message Out:')->pack();
my $text_out = $mw->Text(-width => 35,-height => 5)->pack();
my $text_out_box = $text_out->Scrolled('Text',
-width => 30,
-height => 5,
-scrollbars => 'e')->pack();



$mw->Label(-text => 'Message In:')->pack();

my $text_in = $mw->Text(-width => 35,-height =>5)->pack();
my $text_in_box = $text_in->Scrolled('Text',
-width => 30,
-height => 5,
-scrollbars => 'e')->pack();

my $bottom_frame = $mw->Frame()->pack(-side => 'bottom',-pady => 5);
$bottom_frame->Button(-text => 'Get Line',
-command => \&check)->pack(-side => 'left');


MainLoop;
$text_in->repeat(3000, &check);
sub check {
#chomp($line_in = <INPUT>);
$line_in = <INPUT>;
$text_in_box->configure(-state => 'normal');
$text_in_box->insert('end', $line_in);
$text_in_box->configure(-state => 'disabled');}




Thanks in Advance.
 
I believe what you are asking for is the same as a &quot;tail -f&quot; - here's what &quot;perldoc -q tail&quot; says:

How do I do a `tail -f' in perl?

First try

seek(GWFILE, 0, 1);

The statement `seek(GWFILE, 0, 1)' doesn't change the
current position, but it does clear the end-of-file
condition on the handle, so that the next <GWFILE> makes
Perl try again to read something.

If that doesn't work (it relies on features of your stdio
implementation), then you need something more like this:

for (;;) {
for ($curpos = tell(GWFILE); <GWFILE>; $curpos = tell(GWFILE))
{
# search for some stuff and put it into files
}
# sleep for a while
seek(GWFILE, $curpos, 0); # seek to where we had been
}

If this still doesn't work, look into the POSIX module.
POSIX defines the clearerr() method, which can remove the
end of file condition on a filehandle. The method: read
until end of file, clearerr(), read some more. Lather,
rinse, repeat.

There's also a File::Tail module from CPAN.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
I don't think that will work the <FILE HANDEL> I'm using is actually a socket.

Perl Cook Book
File positions are only meaningful on regular files. Devices, pipes, and sockets have no file position.

Anything else that I could use?
I was looking at ev() but not sure as to weather it would work for what I'm wanting to do.

Any ideas, guidance, & or examples would be appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top