Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: (tye)Re: TK Problem (me again)

by mr_leisure (Beadle)
on Jan 04, 2001 at 23:12 UTC ( [id://49824]=note: print w/replies, xml ) Need Help??


in reply to (tye)Re: TK Problem (me again)
in thread TK Problem (me again)

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Re: Re: TK Problem (me again)
by danger (Priest) on Jan 05, 2001 at 23:58 UTC

    Here is a version with fileevent that works (with some problems):

    # insert widget code here open (LOG, "tail -f log.log|") || die "Nope : $!"; $mw->fileevent(\*LOG, 'readable', \&insert_lines); MainLoop(); sub insert_lines { my $line; if(defined($line = <LOG>)){ if( $line =~ /(CRITICAL|MAJOR)/){ $text->insert('end',$line,$1); } else { $text->insert('end',$line); } }else{ $mw->fileevent(\*LOG, 'readable',''); } }

    The problem being (on my machine) that it sometimes doesn't update even there is data waiting on the pipe. Even the first time it only gets the first line of the tail, then nothing until another line is added to the file (then it gets the remainder of the tail and the newer line). You could work around this using repeat() to check the file yourself every so often -- here's a quicky example that might suffice for your needs:

    # insert widget code here open (LOG, "log.log") || die "Nope : $!"; load_file(5); $mw->repeat(5000,\&tail_file); # check every 5 seconds MainLoop(); sub load_file { my $n = shift || 10; #how many lines to initially tail my @tail; while(<LOG>){ shift @tail if $#tail > $n; push @tail, $_; } for(@tail){ if(/(CRITICAL|MAJOR)/){ $text->insert('end',$_,$1); } else { $text->insert('end',$_); } } } sub tail_file { seek(LOG,0,1); while(<LOG>){ if(/(CRITICAL|MAJOR)/){ $text->insert('end',$_,$1); } else { $text->insert('end',$_); } } }
Re: Re: Re: TK Problem (me again)
by Fastolfe (Vicar) on Jan 05, 2001 at 20:09 UTC
    (Caveat: I've never used Tk in my life, but I think I understand what's going on here by way of context. Take this with a grain of salt.) I don't think you want while (<LOG>) in there. You get to &insert_text when Tk says the filehandle has something waiting to be read. It's probably safe to assume that the amount of data waiting is at least a single line, so you can probably use <LOG> to get it, but don't just turn it into a while loop, or else your function is going to remain "in control" until LOG is depleted (which will be never), and Tk will never have the chance to re-enter its own event loop. Perhaps you want something more like this:
    sub insert_text { local($_) = <LOG>; # process 1 waiting line if (/(CRITICAL|MAJOR)/) { $text->insert('end', $_, $1); } else { $text->insert('end', $_); } }
    If you have 10 lines waiting from LOG, let Tk call your function 10 times. (Unread data still counts as a readable event, I would suspect, but I am assuming Tk implements this using select.)

    You'll have to do something else entirely (is there an 'eof' filevent? an error one?) I suspect to set yourself up to clear the readable event for that file handle, unless insert_text will be called when eof(LOG) is true.

(tye)Re2: TK Problem (me again)
by tye (Sage) on Jan 04, 2001 at 23:22 UTC

    foreach ($textline = <LOG>) {

    doesn't do what you want. Perhaps:

    foreach my $textline ( <LOG> ) { # or, much better: while( defined( $textline= <LOG> ) ) {
    I was thinking of using nearly your original code inside of insert_lines(), assuming that that code set the default color for new insertions. I guess you'd have to restore the default color in the else clause or at the bottom of the loop (or in a continue block).

            - tye (but my friends call me "Tye")
Re: Re: Re: TK Problem (me again)
by ichimunki (Priest) on Jan 05, 2001 at 20:20 UTC
    The fileevent method arguments look suspect. Those do not compare favorably with the examples I found in the Tk::Event POD. That recommends a structure that in this instance would look more like:
    $mw->fileevent( *\LOG, 'readable' => \&insert_txt )
    Which is close to what you have as => and , are interchangeable (although the arrow is semantically easier to follow in this case). You may need to pass a reference to the filehandle instead of the filehandle, but I don't have a good handle on filehandles, so I'd recommend further inquiry after trying something more like the above. And most importantly your callback appears to be wrapped in an anonymous list reference, when all you really need is a sub reference.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://49824]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found