in reply to split file into smaller chunks

Just to echo davorg - you don't really need to have more than one file stream open. The advantage of using a loop in the fashion he suggestions is that you can handle an arbitrary number of chunks.

A side comment as well. I notice you are using the two parameter open and not checking for errors. Best practice is to use a three parameter open and check for errors. Also ">>" appends to the existing contents of the file. If you want to write a file from scratch you should use ">" rather than ">>". See perlopentut. Putting this altogether your open statement should look something like this:

 open(OUT, '>', $file_name) or die "Could not open $file_name: $!";

Best, beth

Replies are listed 'Best First'.
Re^2: split file into smaller chunks
by Anonymous Monk on Jul 17, 2009 at 21:43 UTC
    Yes but, if I don't use >>, how will it add the record each time the counter increases?
    Also, I know and I have seen many times the "die -> Can't open the file" check, but I don't really understand what's the use of it. Is there a chance the the file will not be created with the "open" function?

      Concerning > vs >>. Once you open a file, records will be appended to the end of a file whether you use >> or >. The difference between the two only affects what happens to records already in the file. >> will preserve the current file contents and only add new records to the end. If you use >> and run your program a second time, the file "0-999" will have 2000 records in it. Each record from 0-999 will have two copies: the records 0-999 from the first run and then again at the end the same 1000 records repeated a second time. > avoids this problem by clearing out the old contents. It lets you start the file fresh as if it had been created for the very first time.

      Concerning die.Yes, there is a chance that the file will not be created. Here are some typical reasons:

      • You may not have permission to create files in the directory where you want to place the file.
      • The disk you want to store the file on may be full or you are using an account with a space limits and you have hit your assigned space limit.
      • Someone else is trying to create a file with the same name and the system has "locked" that file name.

      Best, beth