views:

97

answers:

3

I am trying to find filesize using -s operator. I looks like this:

my $filesz = -s $filename

I tried lots of various way but it can not get this size.
However, If I give static content instead of filename, it works fine

For exa:
$filesz = -s "/tmp/abc.txt"

This works fine.
I tried adding " in filename, it didn't work. I removed \n from filename using chomp, problem remains same. Anyone having any idea whats wrong here?

+6  A: 

-s $filename works just fine; the only conclusion is that there's no file with the name contained in $filename. Take a very close look at the contents of $filename, and make sure that your working directory is what you think it is.

hobbs
There were two issues1. There was \n which was not getting chomped properly. It was not visible with print2. I was adding " character around path which was not working with -s.
Jack
@Jack Sounds like your problem was actually with `\r`, not `\n` (and your problem would have been solved by the `:crlf` I/O layer, which translates between `\r\n` and `\n`). Anyway, glad you got it sorted out.
hobbs
A: 

you can try this, it worked for me:

$thefile="somefile";
@stats=stat("$thefile");

@stats now has:
1.Device number of file systems
2.inode number
3.file type and permissions
4.hard link quantity
5.owners UID
6.owners group number of UID
7.device identifier
8.total size in bytes (gives a weird number in linux at least)
9.last access time
10.last modification time
11.inode change time
12.preferred block size for file system IO
13.actual number of blocks allocated



Hermann Ingjaldsson
Yes, I tried that as well. It just that my filename was not proper..
Jack
+1  A: 

As hobbs says, the most likely explanation is that $filename doesn't contain what you think it does.

Based on previous experience, I'd go further than that and hesitate a guess that $filename has a newline character at the end of it. Are you reading the value in $filename from a file or from user input?

davorg
yes, that was one of the issue. I was getting filename from readdir loop.
Jack