in reply to Re^2: Loop walkthrough
in thread Loop walkthrough

beanscake:

There's no loop in your code. Infinite loop means that your program continues to process forever, and that you need to force-close the application. When the application is run with the arguments you specify, it completes and the error message is:

Use of uninitialized value $employ in print at 1094481.pl line 31

Since you've got a series of if/else clauses it simply means that execution took a path where $employ is never set to a valid value. If you uncomment your uncommented lines, then the code works much better. Then you only need to find and fix any missing cases.

Here's an updated version of your program. I tweaked it a little, but the main reason is to add a "bad" value of $employ on initialization. (If a value is supposed to always be set in a subroutine, I like to put something *obviously* bad in during initialization so I notice any possible problems.) The updated code:

roboticus@sparky:~$ cat 1094481.pl #!/usr/bin/perl use strict; use warnings; my $employ = 'FOOBAR'; #------- my $james = $ARGV[0]; my $john = $ARGV[1]; if ($james =~ m/a/) { $employ = "james with grade a"; #pick james }elsif ($john =~ m/a/) { $employ = "John with grade a"; # pick john } elsif ($james =~ m/b/) { $employ = "james doesnt have grade a"; } elsif ($john =~ m/b/) { $employ = "john doesnt have grade a"; } elsif ($james =~ m/b/ && $john =~ m/b/) { $employ = "Both james and john doesnt have grade a \n"; } print $employ, "\n"; roboticus@sparky:~$ perl 1094481.pl b b james doesnt have grade a

Note: it's easy to make this code print "FOOBAR", so be sure to run various test cases to find these cases, and then come up with an appropriate fix.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^4: Loop walkthrough
by beanscake (Acolyte) on Jul 21, 2014 at 14:18 UTC

    thank you roboticus i think i can proceed with the program, that line was my problem but i think am enlighten to proceed :D

Re^4: Loop walkthrough
by beanscake (Acolyte) on Jul 22, 2014 at 16:17 UTC
    #!/usr/bin/perl -w #use strict; #use File::stat; #use LWP::Simple; #two remote site response to check for header '200 OK' #local file to check for altered modification by an intruder' # explanation !! # i want to create a perl programme that will check for two link's hea +der for '200 OK' response then use wget or any available resources to + download a file, if file has been written then do nothing, BUT still + keep checking on set interval basis for the two link header response +. #if the 1st download link is not header response '200 OK' then report +the error, remove the downloaded specified for the link down, and go +for the second link header which must as well be '200 OK, else if on +interval checked the two site specified headers are not '200 Ok' do n +othing print my error or die.................Not Only that,t here is +one more thing to check local file for altered modification' thats my + confusion and am doing alot wrong.. am a newbie to perl want to go h +ard.....help me on this thank you #it seems not to be changing what to download and remove the unavaila +ble.the next available rather it retains the first checked, like i sa +id am newbie to perl so am just trying; my $whereto = $ARGV[0]; # This will be base directory of where to dow +nload my $filename = $whereto.'/'.$ARGV[1]; # the file name my $link1 = $ARGV[2]; # link 1 to check my $link2 = $ARGV[3]; # link 2 to check my $download = $ARGV[4]; #download for link 1 to write my $download2 = $ARGV[5]; #download for link 2 if link 1 not available my $sleep = $ARGV[6]; # sleep interval which i think can be the interv +al of time to recheck my $datetime = localtime; my $success = "\n [+] My downlod response Checker\n [-] Successfully + ...\n [-] Process/PID : \n\n"; my $failed = "\n [?] perl sd.pl <Dir> <file.exention> <link 1> <lin +k 2> <download for link 1> <download for link 2> <check interval>\n\n +"; #perl sd.pl /usr/src/files a.c http://www.site1.eu/mytools/ww.c http:/ +/site2.com/mytools/a.c http://site1.com/mytools/a.c http://site2.com/ +mytools/a.c 10 if (@ARGV != 7) { print $failed; exit(); } else { print $success; } sub link2download { if ( -d $whereto ) { } else { system("mkdir $whereto"); } my $header1 = `curl -A "Mozilla Soldier" $link1 --head --silent` || `w +get -SO- -T 5 -t 1 --user-agent="Mozilla Soldier" $link1 2>&1 | egrep + "HTTP"`; my $header2 = `curl -A "Mozilla Soldier" $link2 --head --silent` || `w +get -SO- -T 5 -t 1 --user-agent="Mozilla Soldier" $link2 2>&1 | egrep + "HTTP"`; my $mod = (stat "$filename")[9]; my $ok = scalar localtime $mod; if ($ok eq "Mon Dec 10 18:30:55 2012" || $header1 !~ /200 OK/ && $hea +der2 !~ /1.1 200 OK/){ #do nothinh } else{ if ($header1 =~ /200 OK/) { system("cd ".$whereto.";fetch ".$download.";wget ".$download.";curl -O + ".$download.";lwp-download ".$download.";touch -t 201212101830.55 ". +$filename.";"); }elsif ($header2 =~ /1.1 200 OK/) { system("cd ".$whereto.";fetch ".$download2.";wget ".$download2.";curl +-O ".$download.";lwp-download ".$download.";touch -t 201212101830.55 +".$filename.";"); } elsif ($header1 !~ /200 OK/) { print "remote $link1 is down $datetime\n"; # then remove the downloa +ded system("cd ".$whereto.";rm -rf ".$filename.";"); } elsif ($header2 !~ /200 OK/) { print "remote $link2 is down $datetime\n"; system("cd ".$whereto.";rm -rf ".$filename.";"); } if ($header2 !~ /200/ && $header1 !~ /200 OK/) { die "$link1\n and $link2\n is down\n"; } print "Local $filename file altered Reloading .$datetime\n"; } } while (1) { sleep ($sleep); link2download(); print "interval\n\n"; # prints to screen }
    The beginning of knowledge is the discovery of something we do not understand.
        Frank Herbert (1920 - 1986)

      beanscake:

      So, a couple of questions:

      1. What's the question?
      2. How would I run it to test it?
      3. What should I see when I run it?
      4. How is this related to hiring James and John?

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

      This is dangerous , filenames can have shell metacharacters in them, don't do this

      system("cd ".$whereto.";rm -rf ".$filename.";");

      Avoid the shell with Path::Tiny

      use Path::Tiny qw/ path /;
      path( $whereto, $filename )->remove;
      path( $filename )->remove;