Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: POE/Tk/Fileevent Strangeness

by cmv (Chaplain)
on Jun 29, 2009 at 15:59 UTC ( #775719=note: print w/replies, xml ) Need Help??

in reply to POE/Tk/Fileevent Strangeness

rcaputo++ Thanks for your help! It makes using POE fun...

Everything you say makes sense to me, and I've redone my test program to use select_read() - see below. It works great on unix, but I get no text showing up either on STDOUT or in the text window under activestate (after re-commenting the use POE statements appropriately). Any pointers here?



use English; use strict; use warnings; use Tk; use POE; #use POE(qw(Loop::TkActiveState)); # Create file descriptor from input file name... my $infile = shift || die "Missing input file"; open(IFILE, "<$infile") || die "Cannot open $infile: $!"; my $ifd = *IFILE{IO}; my $TXT; my $session = POE::Session->create( inline_states=>{ _start=>sub { $TXT = $poe_main_window->Scrolled('Text')->pack; $_[KERNEL]->select_read($ifd, '_GetInput'); }, _GetInput=>sub { if (my $line = <$ifd>) { print STDERR $line; $TXT->insert('end', "-$.- $line"); $poe_main_window->update; } else { $_[KERNEL]->select_read($ifd) }; }, }, ); # Create dummy postback so the session won't die once input is done... my $subref = $session->postback('DontDie'); # Go... $poe_kernel->run();

Replies are listed 'Best First'.
Re^2: POE/Tk/Fileevent Strangeness
by rcaputo (Chaplain) on Jul 14, 2009 at 16:58 UTC

    You should find a different way to read from plain files, for at least two reasons.

    Multiplexing plain files is generally pointless because the operating system always reports them as "ready to read". That is, your select_read() event will fire immediately and constantly. You may as well do:

    _start=>sub { $TXT = $poe_main_window->Scrolled('Text')->pack; $_[KERNEL]->yield('_GetInput'); }, _GetInput=>sub { if (defined(my $line = <$ifd>)) { print STDERR $line; $TXT->insert('end', "-$.- $line"); $poe_main_window->update; $_[KERNEL]->yield('_GetInput'); } },

    Your example works on UNIX because that OS treats everything as a file. Sockets, plain files, pipes, terminals, etc. have their quirks, but deep down UNIX treats them equally.

    As you're discovering, Windows isn't as nice. Even Perl's select() is limited to only working with sockets. This is why you can't get input notification for your plain file handle. The yield() based example above should be portable everywhere.

    A third, bonus issue: You've been mixing unbuffered, multiplexed I/O with buffered reads. This is a classical problem that tends to end badly. For example, the file handle may report not-ready-to-read because the data you're looking for is in the input buffer. The yield() based example will avoid it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://775719]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2017-09-22 12:18 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (262 votes). Check out past polls.