Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

for loop over large BLOCK keeps failing

by cdherold (Monk)
on May 02, 2001 at 11:45 UTC ( [id://77257]=perlquestion: print w/replies, xml ) Need Help??

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

brothers,

the code I have below works great except for one thing, when I put a for loop around it (the first one that goes to $loop=4) it fails completely. Without the loop it goes along and finds all the links on a given webpage, stores those links in an array and then goes into each link to determine if the news article there is something I want to keep ... if it is, the article gets copied and mailed to me.

why does it keep crashing when I put the for loop around the entire program???!! When I run the for loop by itself at top with a simple print statement BLOCK it goes ahead and executes everything as expected, but it fails when I make the entire rest of the program the BLOCK.

I need the entire program BLOCK to be reiterated over and over so I can keep up to date on the press releases, but without having to go and call the program. I intend to put a "sleep" statement in so that it only loops every hour, but if I can't get it to loop then I guess I won't have to worry about that.

the script has been editted for brevity. the loop to 4 is just for testing purposes.

i thought very hard about not including this length of code, but think it is necessary to address the problem. Let me know if I have overstepped proper posting length bounds. thanks, cdherold

for ( $loop=0; $loop=4; $loop++) { use LWP::UserAgent; use HTML::LinkExtor; use URI::URL; $url = "http://www.prnewswire.com"; $ua = new LWP::UserAgent; my @img = (); sub callback { my($tag, %attr) = @_; return if $tag ne 'a'; # we only look closer at written + documents, not images push(@img, values %attr); + } $p = HTML::LinkExtor->new(\&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp; +callback); $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->parse($_[0])}); my $base = $res->base; @img = map { $_ = url($_, $base)->abs; } @img; splice(@img,-20); $links = join(",", @img), "\n"; $count++ while $links =~ /http/gsm; for ($i = 0; $i < $count; $i++) { use LWP::Simple; $body = get("@img[$i]"); + if ($body =~ /inmx/gsmi) { + $mailprog = '/usr/sbin/sendmail'; + $recipient = 'xx@here.com'; + open (MAIL, "|$mailprog -t"); + print MAIL "To: $recipient\n"; + print MAIL "$body\n"; } } }

Replies are listed 'Best First'.
Re: for loop over large BLOCK keeps failing
by perlmonkey (Hermit) on May 02, 2001 at 11:54 UTC
    Your not looping. You are doing an assignment, $loop=4, then quiting the loop. You need to have a conditional statement: $loop == 4 or make it a little more perlish:
    for my $loop (0 .. 3) {
Re: for loop over large BLOCK keeps failing
by busunsl (Vicar) on May 02, 2001 at 12:03 UTC
    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.

      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.
        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 ;-)

Re: for loop over large BLOCK keeps failing
by hugonz (Initiate) on May 02, 2001 at 21:13 UTC
    Please note that you should not be looping the Use statements!! These ones scope the whole script. Hugo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://77257]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found