Has anyone used tail from PPT in a Tk App? (Win32)

J

Johnny Google

I need to use a tail like functionality on a Win32 perl Tk app.
The tail modules (File::Tail, etc) are not ported for Win32.


Someone on another list referred me to the tail function from PPT (Perl
Power Tools). This is a command line file function and not a module...
it seems to do the job when run from the command line.

When trying to implement it as in my other post on fileevent:

open(H, "c:/perl/bin/tail -f -n 25 $pipe_in|") or die "Nope:
$OS_ERROR";
$mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);

- it not only does not work - it leaves the perl process running after
I kill the app.

I have noticed that it appears to trap the interrupt signal - why I
have no idea! - but this makes it hard to kill as a command line and I
think it is what is causing it to continue on after my perl app dies.

I also can't figure out why it isn't working to fill my widget with the
file data...

Any clues - has anyone used this tail function in a perl script before?
In a Tk app? Any other Tail-like functions, modules, Win32 functions?
Thanks,

John
 
E

Eyck Jentzsch

Johnny said:
I need to use a tail like functionality on a Win32 perl Tk app.
The tail modules (File::Tail, etc) are not ported for Win32.


Someone on another list referred me to the tail function from PPT (Perl
Power Tools). This is a command line file function and not a module...
it seems to do the job when run from the command line.

When trying to implement it as in my other post on fileevent:

open(H, "c:/perl/bin/tail -f -n 25 $pipe_in|") or die "Nope:
$OS_ERROR";
$mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);

- it not only does not work - it leaves the perl process running after
I kill the app.

I have noticed that it appears to trap the interrupt signal - why I
have no idea! - but this makes it hard to kill as a command line and I
think it is what is causing it to continue on after my perl app dies.

I also can't figure out why it isn't working to fill my widget with the
file data...

Any clues - has anyone used this tail function in a perl script before?
In a Tk app? Any other Tail-like functions, modules, Win32 functions?
Thanks,

John
I did not use it under windows but for other reasons I implemented a
tail-like function in pure perl:
sub add_tail {
my($file)=@_;
my @stat=stat($file);
my @oldstat=@{$widget{$file}->[1]};
my $fh=$widget{$file}->[1];
# empty the text widget if file is truncated or recreated
if($stat[1] != $oldstat[0] or $stat[7]<$oldstat[2]){
$widget{$file}->[0]->delete(qw/1.0 end/);
$oldstat[2]=0;
}
if($stat[7]>$oldstat[2]){
open(FH, '<', $file);
seek(FH, $oldstat[2], 0);
read(FH, $_, $stat[7] - $oldstat[2]);
close(FH);
$widget{$file}->[0]->insert("end", $_);
$widget{$file}->[0]->SetCursor("end-1char") if($mode);
$widget{$file}->[1]=[$stat[1], $stat[9], $stat[7]];
}
# recall my self in 0.5 sec
$MW->after(500,[\&add_tail, $file])
}
HTH

-Eyck
 
J

Jack D

Johnny Google said:
I need to use a tail like functionality on a Win32 perl Tk app.
The tail modules (File::Tail, etc) are not ported for Win32.
[snip]

Any clues - has anyone used this tail function in a perl script before?
In a Tk app? Any other Tail-like functions, modules, Win32 functions?

You can do this using POE::Wheel::FollowTail. I don't profess to know much
about POE and how it co-exists with Tk. I only know what the few examples
show.

Here is full working example of a tail utility:
######################################
#!/usr/bin/perl

use warnings;
use strict;

# Tk support is enabled if the Tk module is used before POE itself.

use Tk;
use Tk::ROText;
use POE;
use POE::Wheel::FollowTail;

my $FILENAME = "C:/temp/myfile.txt"; #Win32 example
open (TAIL,">$FILENAME") or die;
select(TAIL);
my $ID;

POE::Session->create
( inline_states =>
{ _start => \&ui_start,
gotline => \&ui_read,
ev_clear => \&ui_clear,
writeline => \&ui_write,
stopwrite => \&ui_stop,
}
);

$poe_kernel->run();
exit 0;

sub ui_start {
my ( $kernel, $session, $heap ) = @_[ KERNEL, SESSION, HEAP ];

$heap->{text_widget} =
$poe_main_window->Scrolled('ROText',-scrollbars=>'ose' )->pack;

$poe_main_window->Button
( -text => "Write to File",
-command => $session->postback("writeline")
)->pack;

$poe_main_window->Button
( -text => "Stop Writing to File",
-command => $session->postback("stopwrite")
)->pack;

$poe_main_window->Button
( -text => "Clear Widget",
-command => $session->postback("ev_clear")
)->pack;

$heap->{tail} =
POE::Wheel::FollowTail->new
(
Filename => $FILENAME,
InputEvent => 'gotline');

}

sub ui_read {
$_[HEAP]->{text_widget}->insert('end',$_[ARG0]);
$_[HEAP]->{text_widget}->insert('end',"\n");
$_[HEAP]->{text_widget}->see('end');
}

sub ui_clear {
$_[HEAP]->{text_widget}->delete('1.0','end');
}

sub ui_write {
$ID->cancel if ($ID);
$|=1;
$ID = $_[HEAP]->{text_widget}->repeat(1000,sub {
print TAIL scalar gmtime,"\n"});
}

sub ui_stop {
return unless ($ID);
$ID->cancel if ($ID);
$ID = undef;
$_[HEAP]->{text_widget}->insert('end',"Writing stopped....!!!!!\n\n");
}
######################################

HTH

Jack
 
J

Johnny Google

In your code you use select on the filehandle - I am a little fuzzy on
this but I thought that select will block the file.... I need an
outside app to have the ability to write to the file being read... does
select keep other apps from writing to the file while it is being read?
 
A

Ala Qumsieh

Johnny said:
In your code you use select on the filehandle - I am a little fuzzy on
this but I thought that select will block the file.... I need an
outside app to have the ability to write to the file being read... does
select keep other apps from writing to the file while it is being read?

select() does not block. There is no way through one app to block
another app from modifying a file (AFAIK).

For faster, and more detailed, explanations of Perl's functions, it's
much easier to read the docs that come with Perl:

perldoc -f select

--Ala
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top