Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Odd behavior of file test

by Trudge (Acolyte)
on Oct 03, 2017 at 23:16 UTC ( [id://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 (Archbishop) 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 (Canon) 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 (Chaplain) 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

    Hi,

    use sub Fudge , might be a simple permissions issue

    my $file = '/music.....'; if( -e $file ){ ... } else { printf "<pre>%s</pre>", escapeHTML( Fudge($file) ); }
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (9)
As of 2024-04-23 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found