in reply to for loop over large BLOCK keeps failing

In addition to perlmonkeys answer:

If you use the -w flag, you will get a warning about this error.

And you should use strict!

And I would put all use-statements at the begin of the program, they are executed only once so there is no need to put them in a for-loop.

Replies are listed 'Best First'.
Re: Re: for loop over large BLOCK keeps failing
by cdherold (Monk) on May 02, 2001 at 12:19 UTC
    It's a funny thing ... the program will run through once and mail me the relevant press releases, but then there is some internal error when it tries to do the loop.

    I came to the same results using perlmonkeys for my $loop(0 .. 3){ and  for ($loop=0;  $loop <=4; $loop++) {

    I put strict in at the top with the other uses and that shut everything down. Realize here I am a perl neophyte. I've heard about strict before, but never seen it in use. Could use strict help my problem?

    cdherold

      strict could maybe help with this problem, I don't know.
      But you SHOULD know strict, have a look at the docs here.

      You can have more diagnostic messages from perl if you use diagnostics.

      I spotted one error in your code:
      In the inner loop the line

      $body = get("@img[$i]");
      shoud probably be
      $body = get("$img[$i]");
      since you probably want to get one array element.
        C'mmon ppl, I know you can do better than that.

        cdherold: you should keep you declarations up top. So lower the for-loop below the use.

        Furthermore, keep the sub routine outside the loops. Clean up that one, so it doesn't depend on vars outside the sub (is a good idea in general)

        Look at the MAIL pipe. I don't think open closes MAIL before opening it, but I'm not sure. So *always* check whether a open fails with:

        open BLAH, "some_expr" or die "Could not do some_expr: $!";
        The behaviour cdherold describes could be well due pipe issues. The first print has some effect, but the others are just added to the body or printed to void.

        Well, these are quickies as well. But should give cdherold a start.

        Cheers,

        Jeroen
        "We are not alone"(FZ)

        Thanks for the links to diagnostics and to strict ... and the typo in the code.

        I'm still very confused, however, on why this code doesn't work with the for loop. If I comment out the two lines  for ($loop=0;$loop<=3;$loop++){ and the closing bracket } the code works fine. It seems the only problem is in the for loop statement.

        I can't see my diagnostics ... I assume because I'm running my perl programs via the web where I just get an internal error banner when the program crashes. How do I go about seeing the diagnostics output in this case? Currently what I do to check to see if the program works is to upload it to the server and then run it by going to its url and refreshing the browser. Do you know how I could get the diagnostics to print out? cdherold

      Adding use strict; at the beginning of your script will catch all sorts of typos and bad things because it forces you to declare everything you use. eg:
      my $temp = 'hello'; print $tmp;
      would print nothing, because $tmp is not defined. Of course, you meant to print $temp, but if you don't use strict, this error will go unnoticed by Perl and you may miss it. This example is trivial, but if you had 100 lines of code, it could easily occur!

      cLive ;-)