Foreach and add_watch not working as I would expect



I am having problems with using IO::Watch Gtk2::Helper etc under a foreach loop.

For some reason it will only run the last element of the array. Here
is example code.

sub watch_loop {
my @files2=arr_create();
my ($opts,$prog,$ext)= _;
foreach my $files (@files2){
#my $files= files2[0];
&watch2($files,$opts,$prog,$ext);
}
}

sub watch2 {

my $scroll = Gtk2::ScrolledWindow->new(undef,undef);
my $textview = Gtk2::TextView->new;
my $buffer = $textview->get_buffer;

$scroll->add ($textview);
$vbox->pack_start ($scroll,0,0,0);
$vbox->show_all;
my ($arr1,$opts,$prog,$ext)= _;
print "$arr1\n"; ## This prints OK with elements of array
my @out=split /\./,$arr1;
pop @out;

my $dir='/data/';
#my $ext='test.mpg';
my $out1=join('',@out);
#print $out;
my $newfile="$out1$ext";

open(IN, "-|","ffmpeg -i $arr1 -target pal-vcd -y $newfile 2>&1" )
or die "Failed running perl subprocess\n";

 Glib::IO->add_watch ( fileno(IN), ['in', 'hup'], sub {
         my ($fileno, $condition) = @_;
         if ($condition eq 'hup') {
                 warn "done\n";
                 close IN;
                 return 0;  # uninstall
         }
#          warn "reading...\n";
         my $line;
         sysread IN, $line, 256;
#          warn "read $line\n";
my $end_mark=$buffer->create_mark('end',$buffer->get_end_iter,0);
$buffer->signal_connect(insert_text=>sub{$textview->scroll_to_mark($end_mark,0.0,1,0.0,1.0);});
         $buffer->insert($buffer->get_end_iter, $line);
         return 1;
 });


}

All that seems to happen is that if there are two elements of the
array then two identical scrooll-windows open running the last $var.

Getting a bit confused.

Same thing happens without a sub

Any ideas appreciated



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