Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

print misbehaves

by kettle (Beadle)
on Aug 03, 2006 at 02:34 UTC ( [id://565375]=perlquestion: print w/replies, xml ) Need Help??

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

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'

Replies are listed 'Best First'.
Re: print misbehaves
by friedo (Prior) on Aug 03, 2006 at 02:49 UTC
Re: print misbehaves
by McDarren (Abbot) on Aug 03, 2006 at 02:51 UTC
    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
by kettle (Beadle) on Aug 03, 2006 at 03:11 UTC
    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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://565375]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found