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

I am having a problem checking the existance of a file. I have ran the code used to check for the file in a seperate test file and it works fine. For some reason when present in this script it doesn't execute the needed code, which makes me assume that the -e if returning 0. In my test file I simulated the code as exactly as possible with regards to nested ifs with a foreach and the method in which the filename is described (${save_dir}${filename}). Anyone see what I'm missing?

#!/usr/bin/perl -w use strict; use HTML::SimpleLinkExtor; use LWP::Simple; use Image::Grab; my $save_dir = ">/home/dir/"; my $page = 'http://website.com/webpage.html'; my $link; my $image; my $counter = 0; my $pic = Image::Grab->new(); my $html = get($page); my $extor = HTML::SimpleLinkExtor->new(); $extor->parse($html); my @page_links = $extor->links; my @temp = split(/\//, $page); pop(@temp); my $page_dir = join('/', @temp) . "/"; @temp =""; print "$page_dir\n"; foreach $link (@page_links) { if ($link =~ /jpg$/) { next if $link =~ /small.jpg$/; print $link . "\n"; $counter++; my @temp = split(/\//, $link); my $filename = pop(@temp); #Here is the suspect code if (-e "${save_dir}${filename}") { print "$filename exists, skipping file"; next; } print "Save Dir = ${save_dir}${filename}\n"; print "Image URL = ${page_dir}${link}\n"; $pic->url($page_dir . $link); $pic->grab; open(IMAGE, "${save_dir}${filename}") || die "$!"; print IMAGE $pic->image; close IMAGE; print "${counter}: Saved $link \n\n"; } } print "Finished!\n";

----
Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated failures. Persistence and determination alone are omnipotent. --Calvin Coolidge (1872-1933)

Replies are listed 'Best First'.
Re: Failed file existance check
by rchiav (Deacon) on Apr 29, 2001 at 10:22 UTC
    I don't think you have a directory named this..
    my $save_dir = ">/home/dir/";
    Now I assume that you're using that to open a file for output, but it's not going to pass a file test.

    Rich

      Correct, that was the prob. I carelessly overlooked it when doing my test. Thanks a bunch.
      ----
      Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated failures. Persistence and determination alone are omnipotent. --Calvin Coolidge (1872-1933)
Re: Failed file existance check
by olly (Scribe) on Apr 29, 2001 at 14:06 UTC
    The awnsor Richie gave wasn't clear to me so I spend some time debugging. I got it to work on my own site by removing the > so that it would read:

    my $save_dir = "/home/dir/";

    It might be wise to print the variables you are using at different stages so you can see what they read. That is how I saw that ${save_dir} had the > in it's value. And some die's wouldn't be bad either. Greetings Olly

    Imagination is more important then knowledge -Einstein-