Perl '-s' file test operator problem
I'm debugging a piece of code which uses the Perl '-s' function to get the size of some files.
my $File1 = /myfolder/.../mysubfolder1/document.pdf
my $File2 = /myfolder/.../mysubfolder2/document.pdf
my $File3 = /myfolder/.../mysubfolder1/document2.pdf
($File3
is actually a link to /myfolder/.../mysubfolder2/document.pdf
aka $File2
)
The code which is buggy is:
my $size = int((-s $File)/1024);
Where $File
is replaced with $File1
- $File3
.
For some reasons I can't explain this does not work on every file.
For $File1
and $File3
it works but not for $File2
. I could understand if both $File2
and $File3
would not work, it would mean that the file /myfolder/.../mysubfolder2/document.pdf
is somehow corrupt.
I even added a test if (-e $File)|{
before the -s
to be sure the file exists, but the three files do exist.
There is an even more strange thing: there is an 开发者_如何学Go.htaccess
in /myfolder/.../mysubfolder1/
but no .htaccess
in /myfolder/.../mysubfolder2/
. If it was inverse I would think the .htaccess
would block the -s
call somehow.
Any thoughts?
If -s fails, it returns undef and sets the error in $!. What is $!?
I suppose that if you check the size of that file with "stat" you will get something less than 1024 bytes :) your int((-s $fn)/1024) will return 0 if size is less than 1024
To address the end of your comment, .htaccess
file controls access to files by a web server's request. Once the user requests a URL which executes a valid permissible CGI/whatever script (I'm assuming yoour Perl code is in web context), THAT script has absolutely no permissioning issues regarding .htaccess (unless you actually code your Perl to read its contents and respect them explicitly by hand).
The only permissioning that can screw up your Perl file is the file system permissions in your OS.
To get the file size, your web user needs:
Execute permission on the directory containing the file
Possibly, read permission on the directory containing the file (not sure if the file size is stored in the inode?)
Possibly, read permission on the file iteself.
If all your 3 files (2 good and 1 bad) are in the same directory, check the file's read permissions.
If they are in different directories, check the file's read perms AND directory perms.
Change int((-s $file)/1024)
to sprintf('%.0f', (-s $file)/1024)
- you'll see something then, the file is probably under 1024 bytes, so the int()
will happily return 0.
精彩评论