[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Cool hack of the week




Someone sent me email complaining about how it's a pain in the neck to
have to use $| to enable autoflushing of filehandles.  I was reluctant
to mention the well-known idiom

	select((select(FH), $|++)[0]);

because that's even more disgusting.  I did mention that you can say 

	use FileHandle;
	FH->autoflush(1);

but just saying `use FileHandle' loads *fourteen* files that total
*3389* lines.  You get all these gigantic object-oriented I/O modules
that are totally overkill if all you want is to enable autoflush.

That's where the hack comes in.  Here's my new `Autoflush' module.
Install the following four lines into the file `Autoflush.pm' in your
Perl lib directory:

	sub IO::Handle::autoflush  {
	  select((select($_[0]), $|=$_[1])[0]);
	}
	1;

Now you can say

	use Autoflush;
	FH->autoflush(1);

This loads *one* file with *four* lines.

Thank you, thank you.

Mark-Jason Dominus 	  			               mjd@plover.com

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org