A
A
Alexey2015-04-21 18:04:15
Perl
Alexey, 2015-04-21 18:04:15

Perl: LWP and infinite loop?

I have never written in perl.
There was a need to constantly check approximately 100-200 addresses for availability.
The principle of operation is as follows:
Pick up a list of addresses from the database, check all addresses, cut down inactive ones and start all over again.
Googled lwp, in principle, everything is simple.
But one question interests, whether it will hang up to me in due course the server?
How to clear memory (if necessary), etc. ?
Links to manuals are welcome.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2015-04-21
@pcdesign

It is not necessary to clear storages in a pearl.
This is what the garbage collector does.
If on the knee, then here is such a script on pearl:

use strict;
use warnings;
use utf8;
use feature 'say';
use LWP::Simple;
use HTTP::Request::Common qw(GET);
use open ':std', ':encoding(UTF-8)';

my $ua = LWP::UserAgent->new;

my @add = qw(http://rbc.ru http://habrahabr.ru http://some_dead_site_.net http://yahoo.com);
my %http = map {$_ => 1} @add;
while (1) {
    for my $address (keys %http) {
        my $req = GET $address;
        my $res = $ua->request($req);
        if ( $res->{'_msg'} eq 'OK' ) {
            say "С сайтом $address - все ок";
        }
        else {
            say "Сайт $address - лежит. Удаляем из списка";
            delete $http{$address};
        }
    }
    say "######Спим 20 секунд######";
    sleep 20;
}

And the result of the work:
С сайтом http://yahoo.com - все ок
Сайт http://some_dead_site_.net - лежит. Удаляем из списка
С сайтом http://habrahabr.ru - все ок
С сайтом http://rbc.ru - все ок
######Спим 20 секунд######
С сайтом http://yahoo.com - все ок
С сайтом http://habrahabr.ru - все ок
С сайтом http://rbc.ru - все ок
######Спим 20 секунд######

O
OnYourLips, 2015-04-21
@OnYourLips

I have never written in perl.
There was a need to constantly check approximately 100-200 addresses for availability.

Why did you choose perl if you don't have the task of supporting some very old system?
Ruby! Python! PHP! JavaScript!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question