sub fileSize($) { my($file) = @_; my($size) = -s $file; if (!defined $size) { return -1 } # File does not exist, etc return $size; } sub exampleUse() { my($size); $size = fileSize('missing.txt'); print "Size of non-existant file=$size\n"; $size = fileSize('/etc/passwd'); print "Size of existing file=$size\n"; } exampleUse();
Programming Tips - Perl: get the size of a file
Date: 2023mar10
Language: perl
Q. Perl: get the size of a file
A. Use the -s operator, checking that its defined.
This is nicer that using -e to check if it exists because that does more I/O.