Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Odd behavior of file test

by Trudge (Novice)
on Oct 03, 2017 at 23:16 UTC ( #1200629=perlquestion: print w/replies, xml ) Need Help??
Trudge has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a problem executing code based on the result of a simple file test (-e) of a file I know exists.

if (-e "/music/data/$ArtistName/$cdTitle/$Cover") { print(qq{<p><img src='/music/data/$ArtistName/$cdTitle/$Cover' alt="$A +rtistName-$cdTitle" style="height:240px;width:240px;padding-right:100 +px;float:right"><br clear="right"/></p>}); }

I'm running on an iMac with Apache set up correctly as far as I know - all other scripts work. However the Apache error log tells me this: Cannot serve directory /Users/user/Documents/httpd/public/local/music/data/Ahmad Jamal/Digital Works/: No matching DirectoryIndex So it seems perl interprets the variables up to 'Digital Works' and stops there. It doesn't seem to even see $Cover, and is therefore looking for an 'index.html'. And yes, $Cover is assigned earlier to a file that does exist in the directory. I'm stumped after trying to get this to work, so could someone shed some light on this please?

Replies are listed 'Best First'.
Re: Odd behavior of file test
by Your Mother (Bishop) on Oct 04, 2017 at 00:41 UTC

    You are testing for a file on the filesystem in /music/data/ but a webserver is not a filesystem and is typically configured to serve files from relative locations so when you go to request http://localhost/music/data/ the path is being resolved/normalized to the webroot and ends up /Users/user/Documents/httpd/public/local/music/data/. You need to copy the files into that path or add a symbolic link or adjust your apache config.

      I believe you have it. I changed my request to

      if (-e "/Users/user/Documents/httpd/public/local/music/data/$ArtistNam +e/$cdTitle/$Cover") { print qq{<p><img src="/music/data/$ArtistName/$cdTitle/$Cover" alt="$A +rtistName-$cdTitle" style="height:240px;width:240px;padding-right:100 +px;float:right"><br clear="right"/></p>}; }

      So I need a full system path in my test, but I can use the Apache path to do the print statement (which does show me the image).

      Many thanks for the solution and hopefully others will benefit as well.

Re: Odd behavior of file test
by soonix (Abbot) on Oct 04, 2017 at 05:25 UTC
    Another possibility is Apache (or whichever component) having difficulties with spaces in file (or folder) names, in your case the space between "Ahmad" and "Jamal" (and the other one between "Digital" and "Works").
Re: Odd behavior of file test
by golux (Hermit) on Oct 03, 2017 at 23:34 UTC
    Hi Trudge,

    You'll need to post more of your code (like where you assign to $Cover) for us to be sure, but it certainly looks like $Cover is not getting assigned the way you expect.

    If you're reticent to post code, you can always print out the expected value of $Cover, at various lines, with:

    printf "Now at line %d: \$Cover = '%s'\n", __LINE__, $Cover;

    and see where it's not assigned to the expected value.

    Does that help?

    say  substr+lc crypt(qw $i3 SI$),4,5

      OK, I've tried your suggestion in several places and it all comes back correctly with 'cover.jpg'. There was a section containing a function call, and I was able to print correctly before, during, and after the function.

      Also tried it in my file-test block and no joy! There is definitely something wonky going on.

      Hi golux, and thank you for a quick response. Here is where I assign $Cover ...

      my $Cover="cover.jpg";

      which is quite a bit ahead of this section of the script. And I checked again to make sure the file exists - which it does.

      I will take your suggestion and try printing $Cover in various places and post results here.

Re: Odd behavior of file test
by Anonymous Monk on Oct 04, 2017 at 00:34 UTC


    use sub Fudge , might be a simple permissions issue

    my $file = '/music.....'; if( -e $file ){ ... } else { printf "<pre>%s</pre>", escapeHTML( Fudge($file) ); }
Re: Odd behavior of file test
by sundialsvc4 (Abbot) on Oct 04, 2017 at 12:49 UTC

    This is not a Perl problem:   it is an Apache configuration problem.   You have mod_dir enabled, and Apache thinks that you are asking it to serve a directory, which it takes to mean that it should list the content of that directory.   Apache is not running your Perl program, or passing the URI to it, at all.   I have included a hyperlink above to the relevant Apache documentation.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1200629]
Approved by 1nickt
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2018-08-15 22:27 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (165 votes). Check out past polls.