kidd has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks:
Im looking for some ideas in how is the best way to do this.
Im making a script that takes an URL as an input and checks for its existance. The current code I came up with is this:
#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); use CGI; my $q = new CGI; my $url = $q->params('url'); my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new(GET => $url); my $response = $ua->request($request); my $string = $response->content; $string =~ s/\n//gi; if($string =~ /404 Not Found/ or $string eq ''){ print "$url - Doesn't exist\n"; next; }else{ print "$url - Does exist\n"; next; }
What I do is that I fetch the URL and then check for either the string "404 Not Found" in the $tring or for it to be empty.
The problem with this approach is that there may be a possibilitie that the page has some text that say "404 Not Found" as part of the content but it actually exists.
So I thought that maybe there will be some kind of headers that tell you what kind of response you got...maybe a 302 or a 501, but I got no idea of how can I achieve this.
Thanks
Back to
Seekers of Perl Wisdom