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

Re: [FWP] Benchmarking $|++



"Derek J. Balling" <dredd@megacity.org> writes:

> At 04:42 PM 11/8/99 -0500, Michael G Schwern wrote:
> >More importantly, what if some smart-ass library author decided to
> >write $| = -1;
> 
> Agreed. This falls into the "taking for granted the value of a variable you 
> have not yourself assigned."... I remember learning in like my high school 
> computer-level class that this was a Very Bad Thing.
> 
> Although the $|++ is MUCH easier to type (freely acknowledged), it is 
> definitely very bad form. (IMHO)

Except that it nests properly.  Think of a routine which wants to
unbuffer at the beginning & buffer it at the end.  Here's one way to
do it:

sub bar {
  $| = 1;
  # do stuff
  quux(42);
  # More stuff which depends on unbufferedness
  $| = 0;
}

Simple, elegant, readable code (not to mention 8usec faster).

Now suppose the author of quux decided to do the exact same thing -
start unbuffered output at the beginning of quux, end it before the
return:

sub quux {
  my $forty_two = shift;
  $| = 1;
  # quuxify $forty_two
  $| = 0;
}

Oops.  After the return from quux, $| == 0.

The problem is that "$| = 1" and "$| = 0" don't nest properly in
pairs.  But "$|++" and "--$|" do nest -- so your code does what you
think it does.

True, it doesn't work if somebody says "$|=11".  But at least it
works, if used consistently (although 8usec slower, for each level of
nesting).

PS. I wonder if we should also benchmark "$|=0" vs. "--$|"...

-- 
Ariel Scolnicov        |"GCAAGAATTGAACTGTAG"            |ariels@compugen.co.il
Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem)	\  100% recycled bits!
72 Pinhas Rosen St.    |Tel: +972-3-7658520 (Main office)`--------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555  http://3w.compugen.co.il/~ariels

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe