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

Re: [FWP] Cool hack of the week



Mark wrote:
> 
> Someone sent me email complaining about how it's a pain in the neck to
> have to use $| to enable autoflushing of filehandles.

er, pardon my [possible] ignorance, but what's so hard about $|++ ?

> I was reluctant
> to mention the well-known idiom
>
>  select((select(FH), $|++)[0]);
>
> because that's even more disgusting.

true.

>  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.

talk about disgusting!

> 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.

i can only see this as advantageous if:

- you've got lots of filehandles for which you need to control autoflushing,
since you don't have to use a 'select' before each $|++,

   and

- you're really insistent upon using object-oriented programming for every
little thing

comments?
--
@-------------------------------@--------------------@
| Michael Budash Consulting     | (707) 252-7670     |
| Perl, Javascript, PHP, MySQL  | (603) 250-8679 fax |
| Official Extropia Developer   | mbudash@sonic.net  |
@-------------------------------@--------------------@

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