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

The perl code I've created is designed to accept an input (a surname) from a user via a web form, convert that into a directory and filename and then print a link to that file. Problem I'm having is checking on whether the files exists before providing the link. I am obviously making some kind of syntax mistake with the "if" statement as the program still outputs the print result even when a file doesn't exist. Here's the statement with the accompanying print statements shortened:

sub printForm{ @ParseFile = split //,"$FILENAME"; $Path = "$ParseFile[0]/$ParseFile[1]/$ParseFile[2]"; if (! -e "http://www.surnameweb.org/registry/$Path/$FILENAME") { print "<p align=\"center\"><A HREF=\"http://www.surnameweb.org/r +egistry/$Path/$FILENAME\">$CRITERIA</A>"; } else{ print "\n"; } }


Wisdom shined upon this problem would be appreciated...

Thanks!
Perdrix

Replies are listed 'Best First'.
Re: Checking if File Exists
by DarkGoth (Acolyte) on Feb 22, 2001 at 14:03 UTC
    Hi,

        If you are interrested in checking if a link on a Web file is broken or not :
        I advice you to read the Perl CookBook of O'reilly on Chapter 20.7 :


    #!/usr/bin/perl -w # churl - check urls use HTML::LinkExtor; use LWP::Simple qw(get head); $base_url = shift or die "usage: $0 <start_url>\n"; $parser = HTML::LinkExtor->new(undef, $base_url); $parser->parse(get($base_url)); @links = $parser->links; print "$base_url: \n"; foreach $linkarray (@links) { my @element = @$linkarray; my $elt_type = shift @element; while (@element) { my ($attr_name , $attr_value) = splice(@element, 0, 2); if ($attr_value->scheme =~ /\b(ftp|https?|file)\b/) { print " $attr_value: ", head($attr_value) ? "OK" : "BAD", + "\n"; } } }


    Here's an example of a program run:
    % churl http://www.wizards.com http://www.wizards.com: FrontPage/FP_Color.gif: OK FrontPage/FP_BW.gif: BAD #FP_Map: OK Games_Library/Welcome.html: OK


(ichimunki) re: Checking if File Exists
by ichimunki (Priest) on Feb 22, 2001 at 04:21 UTC
    If you are intending to check the link at a remote site, you will need to use LWP::Simple or something like that to do an HTTP transaction. Otherwise simply using "$Path/$FILENAME" will get you to your file.
Re: Checking if File Exists
by unixwzrd (Beadle) on Feb 22, 2001 at 13:52 UTC
    I suspect you want this:
    if ( -e "registry/$Path/$FILENAME" ) { print "<p align=\"center\"><A HREF=\"http://www.surnameweb.org/regi +stry/$Path/$FILENAME\">$CRITERIA</A>"; } else{ print "\n"; }
    Though the "else..." is probably unnecessary.

    Hope that helps...

    Mike

    "The two most common elements in the universe are hydrogen... and stupidity."
    Harlan Ellison