<?xml version="1.0" encoding="windows-1252"?>
<node id="201541" title="Checking for an existing URL" created="2002-09-29 10:02:20" updated="2005-07-04 01:55:32">
<type id="115">
perlquestion</type>
<author id="176753">
kidd</author>
<data>
<field name="doctext">
Hello monks:

Im looking for some ideas in how is the best way to do this.
&lt;p&gt;
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:
&lt;p&gt;
&lt;code&gt;
#!/usr/bin/perl -w

use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI;

        my $q = new CGI;

        my $url = $q-&gt;params('url');

        my $ua = LWP::UserAgent-&gt;new;
        my $request = HTTP::Request-&gt;new(GET =&gt; $url);
        my $response = $ua-&gt;request($request);


        my $string = $response-&gt;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;
                 }
&lt;/code&gt;
&lt;p&gt;
What I do is that I fetch the URL and then check for either the string "404 Not Found" in the &lt;code&gt;$tring&lt;/code&gt; or for it to be empty.
&lt;p&gt;
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.
&lt;p&gt;
So I thought that maybe there will be some kind of headers that tell you what kind of response you got...maybe a &lt;b&gt;302&lt;/b&gt; or a &lt;b&gt;501&lt;/b&gt;, but I got no idea of how can I achieve this.
&lt;p&gt;
Thanks</field>
</data>
</node>
