print misbehaves
kettle
created: 2006-08-02 22:34:23
How, exactly does 'print' work?

I read the relevant documentation, but can't seem to figure out what is going on with my script. I am processing a very large file, which is stored in a nested hash, which I am eventually printing out after various manipulations (ok, so that sounds very unclear, but the code is too long to post, and I'm not really interested in having my problem explicitly solved by someone else - I'd like to know a bit about the perl digestive tract). At certain points output hangs, and the print statements never get executed, however if I include an extra 'print' statement for the hash keys/values I am trying to process and print, the later keys/values which otherwise hang, print out properly. I am adding/changing nothing to the body of the script, other than this extra print statement.

So how exactly does the print statement work? Is it flushing memory/data that some poorly written code is otherwise accumulating? I'd really like to know since I've never seen this before. Thanks!

2006-08-03 Retitled by GrandFather, as per Monastery guidelines
Original title: 'print'

Re: print misbehaves
created: 2006-08-02 22:49:25
Sounds like you're suffering from buffering.

Read that and all will be well with the universe.

Re: print misbehaves
created: 2006-08-02 22:51:09
You may be suffering from buffering.

Also see perlfaq5

If that's the case, then you need to set the autoflush flag. This is generally achieved by just doing $|++;
However, if you are printing to a filehandle then you may need to select that filehandle first. See the links above for a more in-depth discussion.

Hope this helps,
Darren

Re: print misbehaves
created: 2006-08-02 23:11:37
Awesome! that was just what I was looking for. That was basically what I figured was happening, but I wasn't sure what options I had in dealing with it. Thanks a lot - my buffering suffering has come to an end!

perlmonks.org content © perlmonks.org and friedo, kettle, McDarren

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03