I need an infite loop. Basically what I'm trying to do is a tail...
reading in entire file and when I get to the end of the file; sleep
for X Seconds and then see if there is any more lines on the end of the
file is unread, else sleep again and repeat.
Yeah, that is why in my first example I used a piped-open to tail the
file, then read that filehandle with fileevent. That essentially forks
off the tail process. Now on windows, fork is emulated using threads,
so you could use the tail code in a thread, and read it thru shared
variables.
Since there are so many ways to do this, why don't you check out
http://perlmonks.org/?node=tail+win32&go_button=Search
and read the links where some win32 savvy perlmonks discuss it.
I'm a linux guy, and I'm probably missing some win32-expertise.
I don't need to update the window with the entire file, only need to
update the window with the new lines (appeneded at the end of the
buffer).
Yeah, the problem I see in my previous example is the repetitive
opening /closing the file. It would be best to open it just once.
But see below for a hack.
for example, in a normal text console under Win32 I can do this:
for(;

{
while ( <FILE> ) {
my $line=$_;
chomp($line);
print ("$line\n");
}
sleep (1);
If you put this sleep in a thread, it would be Ok. But in a Tk app,
you should not use sleep, use a non-blocking delay
$mw->after(1000); # will simulate a 1 second delay without blocking
FILE->clearerr();
}
That reads in entire file, when it gets to end, it sleeps for 1 second.
It then loops back and starts reading where it left off (if there are
any new lines it prints them) then repeats.
I don't need this "event handler loop" stuff getting in my way. I
Sorry, but to do it correctly, you need fileevent, or IO::Select which
essentially does the same thing ( but you must account for blocking the
event-loop with IO::Select, like using after and update in the loop as
shown below).
tried forking it off (so it could run in a differant thread and update
whatever it needs) but that causes the GUI to hang.
I want to control directly what I'm doing or not doing with the file.
It's not the EventHandler's bussiness what I'm doing with this file in
the privacy of my own code.
You can try ( or use an IO::Select can_read loop )
for(;

{
while ( <FILE> ) {
my $line=$_;
chomp($line);
print ("$line\n");
$textbox->insert('end', "$line\n");
}
$mw->after(1000); # will simulate a 1 second delay without blocking
$mw->update; # force the event loop to update
FILE->clearerr();
}
Can I just do a fork and say "Here Mr. TK GUI, you run in a differant
thread and do whatever you need to do. I'll send you data if I want
you to update something, else stop getting all up in my code like
that".
Basically I want a nice GUI interface with fancy buttons and stuff and
then imbedded in the GUI a window that acts like a "stupid console".
Does that make sense?
Basically I want to do this:
http://tailforwin32.sourceforge.net/
But in Perl.
TK/Win32, I don't care. Just some type of nice GUI interface.
Here is a discussion of how someone on perlmonks did a win32
tail. See the code in the reply by msemtd
http://perlmonks.org/?displaytype=print;node_id=297848;replies=1
#########################################################
I would probably use a thread myself. Here is a simple
thread example. It has a drawback, that it takes a few seconds
to re-pickup on the output if the pumper script stops and restarts.
####################################################
#!/usr/bin/perl
use warnings;
use strict;
use threads;
use threads::shared;
# must setup thread code before any Tk code is used
# to avoid Tk thread-safety problems
my @logdata : shared;
my $thread_die : shared;
@logdata = ();
$thread_die = 0;
my $thread = threads->new( \&work );
############################################
use Tk;
my $mw = MainWindow->new(-background => 'gray50');
my $tframe = $mw->Frame( -background => 'gray50',-borderwidth=>10 )
->pack(-side =>'top' ,-fill=>'y');
my $bframe = $mw->Frame( -background => 'gray50',-borderwidth=>10 )
->pack(-side =>'bottom',-fill =>'both' );
my $text = $tframe->Scrolled("Text",
-scrollbars => 'ose',
-background => 'black',
-foreground => 'lightskyblue',
)->pack(-side =>'top', -anchor =>'n');
my $exit_button = $mw->Button(-text => 'Exit',
-command => sub{
$thread_die = 1; #kill thread
exit;
})->pack();
my $timer = $mw->repeat(1000, sub{
lock( @logdata );
my @in = @logdata; #copy it
@logdata = (); #clear out old log lines
# cond_broadcast( @logdata );
$text->insert('end', "@in");
$text->see('end');
});
MainLoop;
##################################################################
sub work{
$|++;
open(FH,"< z.log") or die "$!\n";
while(1){
while(<FH>){
push @logdata, $_;
if( $thread_die == 1 ){return} #kill thread
}
}
}
#####################################################################
__END__
############ and the pumper script ############################
#!/usr/bin/perl
use warnings;
use strict;
$| = 1;
open (ZH, "> z.log") or die "$_\n";
# autoflush ZH
my $ofh = select(ZH);
$| = 1;
select($ofh);
while(1){
print ZH time."\n";
print '#'; #action indicator
select(undef,undef,undef, 1);
}
__END__