Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to Properly Clear Variables for reuse in a loop

by Anonymous Monk
on Apr 23, 2009 at 21:31 UTC ( #759676=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have not done a lot of Perl programming other than quick knock-offs for system-specifc tasks, so my Perl knowledge is pretty basic. I created a program which goes out and runs a CGI program which returns some data. From there I can do what I want with it. The program works fine. However, I wanted to run this continuously, with a sleep of say 3 minutes. When I put the code in a while loop, it runs, but it returns the exact same data as before, even if I know that the data is different (which I can check by calling it in a browser). Obviously I need to clear something out, but I am at a loss as to what to do.
#!perl -w use Win32::Internet; my $addr = "http://somewebaddress.com/prog.cgi?parameters"; my $INET = new Win32::Internet(); my $url = $INET->FetchURL($addr); $INET->Close(); print $url; ... etc ...
This works fine. However, if I try enclosing it in a while ...
#!perl -w use Win32::Internet; my $addr = "http://somewebaddress.com/prog.cgi?parameters"; while (1) { my $INET = new Win32::Internet(); my $url = $INET->FetchURL($addr); $INET->Close(); print $url; sleep 180; }
It runs fine, but the data ($url) is exactly the same - and I know it isn't. Obviously I need to clear the variables out. I've tried setting them to undef before the sleep but that doesn't have any effect. Appreciate any guidance.

Replies are listed 'Best First'.
Re: How to Properly Clear Variables for reuse in a loop
by ikegami (Pope) on Apr 23, 2009 at 22:01 UTC

    FetchURL surely does a GET, and GET requests are subject to caching unless the response includes headers to forbid it or to limit it.

    LWP does no caching. You could use that instead.

Re: How to Properly Clear Variables for reuse in a loop
by Anonymous Monk on Apr 23, 2009 at 22:06 UTC

    I don't see anything in particular wrong with your code, I assume what's going on is that the library Win32::Internet connects to does caching on HTTP gets where the params are static. I know i've run into similar issues with older verions of microsoft's XMLHttpRequest object.

    I'd try some thing like LWP or WWW::Mechanize which allow for more control over your request, and should be able to accomplish what you need. Another thing that may work is preventing caching by adding something like this:

    my $url = $INET->FetchURL($addr."&nocache=".time());
      Fantastic! Using this worked:
      my $url = $INET->FetchURL($addr."&nocache=".time());
      Really appreciate the responses! Thanks!
Re: How to Properly Clear Variables for reuse in a loop
by linuxer (Curate) on Apr 23, 2009 at 22:12 UTC

    Your variables are declared with my within the while loop. So they are re-initialized on each run and shouldn't know of their previous value.

    And you assign a value to $url on each run; so it should even work without the my inside the loop, as the content of $url is overwritten on each run of the loop.

    Ergo: If you see the same output per run, the problem is in FetchURL as others already mentioned.

    $ perl -wle ' > my $i = 1; > while ( $i < 5 ) { > my $j = $i++; > print $j; > }' 1 2 3 4 $
    $ perl -wle ' my $i = 1; my $j; while ( $i < 5 ) { $j = $i++; print $j; }' 1 2 3 4 $

    You can see, each print prints the value of $i and $j doesn't "remember" its previous content.

    update: sentence rewritten

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://759676]
Approved by planetscape
help
Chatterbox?
shmem is an "instant asshole - just add alcohol" :-P
[james28909]: you guys/gals are awesome. thanks for the wisdom.

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-05-20 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?