[Perl.sig] Perl tip: Buffering and IO::Handle
litss.coord at anu.edu.au
Tue Sep 13 15:19:29 EST 2005
==== Fun with Dead Languages ====
Dr Damian Conway, author of "Object Oriented Perl" and "Perl Best
Practices", highly acclaimed speaker and Perl 6 language designer, will
be presenting at this week's Melbourne Perl Mongers meeting.
What: Melbourne Perl Mongers
When: 6:30pm, Wednesday 14th September, 2005
Where: Level 8, myinternet house, 14 Blackwood St, North
If you're in the area and interested in attending a night of fun and
laughter; come along. Invite your workmates, friends and colleagues.
==== Buffering and IO::Handle ====
Sooner or later most Perl programmers encounter the concept of buffering
on filehandles, and more often than not it's by accident. Perl's rule
for buffering output is very simple: if output is sent to anything
that's not a terminal, then it will be buffered by default.
Buffering is generally a good thing, by sending output to the operating
system in managable chunks we reduce the amount of context switching and
overheads associated with output. That makes our system more efficient
and responsible, and all without any effort on behalf of the programmer.
Our problems arise when we want output to happen now, rather than when
it becomes efficient to do so. Common situations include when we're
writing to something that in turn is outputting to a terminal, or to a
logfile where it's critical we know exactly what stage our program has
reached. In these situations, we'd like to turn buffering off.
The traditional way to disable buffering on a filehandle is to use a
``select'' statement, and Perl's special $| variable:
my $old_fh = select($fh_to_unbuf); # Select FH to unbuffer.
$| = 1; # Unbuffer that FH.
select($old_fh); # Re-select the old FH.
Unfortunately the code above isn't particularly clear, even with
comments explaining each step. If we leave out the last step, we can
accidentally change the default output for ``print'' statements, which
can be a maintenance nightmare.
Luckily, there's a better way, without requiring changes to your
existing code. All of Perl's filehandles act like members of the
``IO::Handle'', whether they're contained in a scalar, or an old
bareword handle like ``STDOUT'' or ``LOGFILE''. That's right, anything
you can do using ``IO::Handle'' can be done on a regular filehandle.
Now unbuffering a filehandle can be done easily and cleanly:
# Unbuffer a scalar filehandle.
$fh_to_unbuf->autoflush(1); # Make unbuffered.
# Unbuffer an old-style filehandle.
``IO::Handle'' also provides a number of methods for common problems,
such as explicitly flushing an output handle:
or printing a single record in an unbuffered fashion:
$fh->printflush("Checkpoint: Photon torpedos armed");
``IO::Handle'' is a standard Perl module, and provides many methods that
make working with filehandles simpler and more maintainable. To learn
more, simply use ``perldoc IO::Handle''.
==== Upcoming Courses in Canberra ====
Introduction to Perl: 1st - 2nd November
Intermediate Perl: 3rd - 4th November
==== Upcoming Courses in Melbourne ====
Perl Best Practices: 14th - 15th November
Understanding Regular Expressions: 22nd November
Web Development with Perl: 1st - 2nd December
==== Upcoming Courses in Sydney ====
Introduction to Perl: 20th - 21st September
Intermediate Perl: 22nd - 23rd September
Web Development with Perl: 28th - 29th November
==== Corporate Courses ====
Do you have a large group, or the need for training at a particular
time? Perl Training Australia is happy to arrange a course in the
and place that best suits you. For more information read our page on
Corporate Courses at http://perltraining.com.au/corporate.html or
us on +61-3-9354-6001.
This Perl tooltip and associated text is Copyright Perl Training Australia.
You may freely distribute this text so long as it is distributed in full
with this Copyright noticed attached.
If you have any questions please don't hesitate to contact us:
Email: contact at perltraining.com.au
Phone: 03 9354 6001
Perl-tips mailing list
To change your subscription details visit:
More information about the Perl.sig