Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am very new to Perl and may be asking something quite rudimentary. Forgive me. After opening a file for append write
 open(FILE,$write_file) || die "file could not be opened for update";

I received the error of being 'out of memory' before the following line
  my $date = strftime('%D',localtime);.

Can you post any solutions or at least somewhere to begin my search?

Thanks

Replies are listed 'Best First'.
Re: Out of Memory after file open for append
by pjf (Curate) on Oct 08, 2001 at 12:39 UTC
    Having a bit more of your program (like the lines between that open and the strftime line) would probably help, as both the lines that you've listed are fine, and there are many things that could potentially chew up all your memory. Common things are slurping an entire very large file, infinite recursion, or building an incredibly big list with something like (1..1000000000).

    On a completely different track, the '%D' format for strftime prints the date in MM/DD/YYYY format, which is great if you're in America, but terribly confusing in many other places in the world (such as Europe or Australia). It also uses a 2-digit year, and we all know how much trouble people got into for using that last century.

    If you wish to have an unambiguous, internationally readable date, then "%Y-%m-%d" is highly recommended.

    As a different aside, evaluating localtime() in a scalar context also returns an unambiguous, human readable date.

    Cheers,
    Paul

Re: Out of Memory after file open for append
by blakem (Monsignor) on Oct 08, 2001 at 12:33 UTC
    To open your file for appending you'll need to use something like:
    open(FILE,">>$write_file") or die "file '$write_file' could not be opened for update: $!";
    </code> I added the filename to the error message along with '$!' which is the OS level error. It can be very helpful for tracking down why your open failed.

    See open for more details.

    Update: lemming points out that $write_file might already contain the '>>', i.e. $write_file = '>>/my/filename' In that case, adding an extra '>>' would be superfluous and incorrect.... w/o more code, we can't tell one way or the other.

    -Blake