I haven't looked deeper, but some comments may help you find the offending code:
- it looks like you're reading an entire chunk of size $fsize into memory to write it to disk — maybe you should consider read and write with a buffer, something like: turn 1GB file into files of 100MB but reading/writing in 10MB chunks. It may make more sense if your files are larger than your RAM memory.
Since you're using read, the write function would be more appropriate as a counterpart than print.
- Since you're using read, you may take advantage of the return which is the number of bytes read to use it for counting how much was actually read so far.
- With such a scheme of reading blocks until you fill a part and then going on until you exhaust the original file, you didn't even need to query for the file size.
And the error you're getting should be related to this statement:
$fsize-=$size;
that will produce a negative size eventually if
$fsize is not a multiple of
$size.
Update: as pointed by johngg, I messed up things thinking about a pair read/write when there's only sysread/syswrite in Perl. print is just fine as a counterpart of read.