in reply to When can the character length of a Perl Scalar become an issue?

There are a few times in my past experience where this sort of seemingly random thing has happened to me. Usually it was that I had not properly closed the file I had been writing to, or that the write buffer had not, for whatever reason, entirely been emptied--the latter problem of which was alleviated by turning the print handle "hot":

$| = 1;

(Place that somewhere prior to your print task--typically near the top of the script for me.)

Blessings,

~Polyglot~

  • Comment on Re: When can the character length of a Perl Scalar become an issue?
  • Download Code

Replies are listed 'Best First'.
Re^2: When can the character length of a Perl Scalar become an issue?
by haukex (Archbishop) on Sep 21, 2023 at 07:25 UTC
    Usually it was that I had not properly closed the file I had been writing to, or that the write buffer had not, for whatever reason, entirely been emptied

    Perl flushes buffers when closing a file and closes files when exiting, so this is almost certainly not the problem, especially since the OP says that the closing tag still gets written after the truncation.

    the latter problem of which was alleviated by turning the print handle "hot": $| = 1;

    This won't help not only due to what I said above, but also because $| refers to the currently selected filehandle, so STDOUT by default; it won't do anything for code like OP showed. Even though it won't help here, for completeness, $filehandle->autoflush(1) is one way of controlling it.

      This won't help not only due to what I said above, but also because $| refers to the currently selected filehandle, so STDOUT by default; it won't do anything for code like OP showed. Even though it won't help here, for completeness, $filehandle->autoflush(1) is one way of controlling it.

      Yes. I'm always saddened to see unfortunate old Perl globals, such as $|, still being used today.

      For more detail on this topic see: Re: what is the meaning of $| in perl? (Buffering/autoflush/Unicode/UTF-8 References)