I am trying to read some file(s), get its size, and if the file(s) are larger than ...
I think you want to change the order of the operations: get the file size first, then if it's bigger than $last_maxsize, open it, read $last_maxsize bytes, write that to a new version of the file:
if ( -s "$path_only/$file_name" > $last_maxsize ) {
local $/ = \$last_maxsize;
open IN, '<', "$path_only/$file_name";
$_ = <IN>;
close IN;
open OUT, '>', "$path_only/$file_name";
print OUT;
close OUT;
}
Looking at the code you posted, I was puzzled by this part:
while ( my $new_record = <$last_in> ) {
next if -s $new_record > $last_maxsize;
...
Look up "perldoc -f -X" to read about the "-s" function -- it takes either a file handle or the name of a file. Since $new_record is neither of these things in your code, I would expect "-s" to always return 0. And since this is never greater than $last_maxsize, you will be opening an output file every time. And why use a while loop at all, if you only intend to read a single record of $last_maxize bytes?
The last issue, of course, it whether it's really wise to always truncate the file to a specific number of bytes. If it's text data, you might want to respect line boundaries (or at least word boundaries) -- and if the text is in some non-ASCII encoding (e.g. has utf8 wide characters) you should be sure to respect character boundaries; if it's compressed data, truncating it at all will make it impossible to uncompress; and so on... It's actually hard to imagine any kind of data that wouldn't suffer badly from an arbitrary fixed-length truncation like this. |