Re: Gtk::Gdk::input_add and CPU racing



David wrote:

Here is how am actually using this(still simplified):
#####################################################

$button->signal_connect("clicked", \&do_ls);

main Gtk;
exit( 0 );

sub do_ls 
{
  open(LS, "ls 2>&1 |");
  my $id = Gtk::Gdk->input_add(fileno(LS),['read'],\&list);
}

sub list
{
  while(<LS>)
       { $statusbox->insert( $font, undef, undef, $_ );}
  Gtk::Gdk->input_remove($id);
}
####################################################

Am I taking the wrong approach?

Slightly ;) First: you *really* should use

  use strict;

at the top of your program, then you would have found your mistake by 
yourself ;)

$id is lexically scoped until the end of your 'do_ls' subroutine, so 
it's not known in the list sub. With "use strict" Perl complains about 
accessing an unknown resp. not declared variable. Pass $id to your list
subroutine, e.g. by using a closure:

  my $id;
  $id = Gtk::Gdk->input_add(fileno(LS),['read'], sub { list($id) } );

  sub list {
    my ($id) = @_;
    ...
  }

Also consider that your LS is global. Better use a filehandle and pass 
it as an argument, too.

Regards,

Joern

-- 
LINUX - Linux Is Not gnU linuX




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]