deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

When I run this code snippet, the program is supposed to open waytemp, read in the data, then replace NEW with OLD on the file top and reprint the data. The problem I am encountering is that the program is not printing the data to the waytemp file, the second time around. Im suspecting that it is not entering the for loop or encountering an error when it enters it. I think I need some fresh eyes to look at the code.
#!/usr/bin/perl -w use strict; use warnings; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; use threads; use threads::shared; my $die: shared = 0; my $verification_control: shared = 0; my $waytemp_local: shared = 'C:\Users\deadpickle\Desktop\files\waytemp +'; my $wayfinal_local: shared = "/home/deadpickle/Desktop/project_downloa +ds/wayfinal"; my $thread_verification = threads->new( \&verification); my $timer2 = Glib::Timeout->add(100, \&messagedialog); my $window = Gtk2::Window->new('toplevel'); my $button = Gtk2::Button->new( 'GO'); $window->signal_connect(delete_event => sub { $die = 1; $thread_verifi +cation->join; Gtk2->main_quit}); $button->signal_connect('button-press-event' => sub { $verification_co +ntrol = 1}); $window->add( $button); $window->show_all; Gtk2->main; sub verification { while(1){ if ( $die == 1 ){ goto END }; if ( $verification_control == 1 ){ for(;;){ print "GO\n"; my $count; if($die == 1){ goto END }; #open waytemp and load the waypoint locations then clo +se the file; the values will remain in the variables for later use open TEMP, '<', "$waytemp_local" or die; my @temp = <TEMP>; print "@temp\n"; chomp @temp; close TEMP; #count the lines in the waytemp file; disregard the fi +rst line since it is just the "time" stamp my $line = @temp - 1; #check if the waytemp file is NEW; checks only the fir +st line in the file if ( $temp[0] eq 'NEW') { #print the waypoints to the waytemp file; add OLD +to the top open TEMP, '>', "$waytemp_local" or die; print TEMP "OLD\n"; for ( $count = 1; $count >= $line; $count++) { print TEMP "$temp[$count]\n"; } close TEMP; #run the dialog that verifies the waypoints; needs + user input $verification_control = 2; if ( $verification_control == 2) { for (;;) { #if 'yes' if ( $verification_control == 3) { last} #if 'no' if ( $verification_control == 4) { goto NO +} } } #upload the new wayfinal file copied from the new +waytemp open FINAL, '>', 'C:\Users\deadpickle\Desktop\file +s\wayfinal' or die; for ( $count = 1; $count >= $line; $count++) { print FINAL "$temp[$count]\n"; } close FINAL; NO: } $verification_control = 1; } $verification_control = 0; } } END: } #Message Dialog box that asks to verify the new waypoints recieved fro +m the server sub messagedialog { #Run the message dialog if the waytemp file is stamped with a "NEW +" if( $verification_control == 2) { my $messagedialog_verification = Gtk2::MessageDialog->new( und +ef, 'destroy-with-parent', 'question', 'yes-no', "Verify waypoints?") +; $messagedialog_verification ->set_position( 'center'); my $response = $messagedialog_verification->run; #if the file is verified $verification_control = 3 if $response eq 'yes'; #if the file is not verified $verification_control = 4 if $response eq 'no'; $messagedialog_verification->destroy; } return 1; }

Replies are listed 'Best First'.
Re: Gtk2: not entering for loop
by zentara (Cardinal) on Aug 16, 2007 at 11:55 UTC
    To get the files to print correctly, you need to change the 2 lines where you have
    for ( $count = 1; $count >= $line; $count++) { # to for ( $count = 1; $count <= $line; $count++) { # ^^^
    once that is done, you have some error in your nested loop logic, and your thread keeps looping.....it's too early in the morning for me to track down your nested logic. :-) You probably want to put your thread to sleep when it is done verifying a request. You can check that out by adding this at your NO: label
    NO: $verification_control = 0; goto END;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum