perl_style_dollar_bar_plus_plus_considered_harmful

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



Path: nntp.stanford.edu!newsfeed.stanford.edu!nntp.cs.ubc.ca!newsfeed.direct.ca!look.ca!newsfeed.concentric.net!global-news-master
From: abigail@delanet.com (Abigail)
Newsgroups: comp.lang.perl.misc
Subject: Re: Why $|++ instead of $|=1 ?
Date: 05 Jul 2000 22:20:54 EDT
Organization: Abigail's Kinderboerderijen
Lines: 38
Message-ID: <slrn8m7s9q.ibb.abigail@alexandra.delanet.com>
References: <8k0m4d$13o$1@panix3.panix.com>
Reply-To: abigail@delanet.com
NNTP-Posting-Host: 216.112.169.154
X-Date: MMDI September MCMXCIII
X-HTTP: http://purl.oclc.org/NET/Abigail
X-Revision: $Revision: 1.2 $
X-PGP-Fingerprint: 76 2E 82 7A 69 93 5F 97  AE 01 80 75 67 F3 45 D0
User-Agent: slrn/0.9.6.2 (Linux)
Xref: nntp.stanford.edu comp.lang.perl.misc:323641

kj0 (kj0@mailcity.com) wrote on MMDI September MCMXCIII in
<URL:news:8k0m4d$13o$1@panix3.panix.com>:
== 
== 
== Is there any reason why $|++ would be preferable over $|=1?


There isn't. In fact, $|++ is really, really bad. It's a classical
example of cargo cult programming. Someone once used it, and that got
copied all over.

The problem with $|++ is that it turns of buffering only if $| is 0.
If for some reason $| is -1 (that is, buffering is already off), you
turn buffering *ON*, getting opposite effects of what you want.

Now, you may say who would sets $| to -1? Well, if you use $|++ to disable
buffering, wouldn't it make sense to use $|-- to enable buffering again?
And if your program has $| ++, $| -- and $| ++ in that order, things go
quite out of hand if you remove the first $| ++.


If you just use $| = 1 and $| = 0, you never, ever have to chase your
program to see what $| was, nor do you have to chase your program for
subsequent modifications if you take out an assignment to $|.

Another big advantage of $| = 1; is that you can put 'local' in front
of it, where you can't with $| ++.

$| ++ is obscure and bad. $| = 1 makes it instantly clear what the
effects are.



Abigail
-- 
$_ = "\x3C\x3C\x45\x4F\x54\n" and s/<<EOT/<<EOT/ee and print;
"Just another Perl Hacker"
EOT

Path: nntp.stanford.edu!newsfeed.stanford.edu!paloalto-snf1.gtei.net!paloalto-snh1.gtei.net!su-news-hub1.bbnplanet.com!news.gtei.net!newsfeed.direct.ca!look.ca!newsfeed.concentric.net!global-news-master
From: abigail@delanet.com (Abigail)
Newsgroups: comp.lang.perl.misc
Subject: Re: Why $|++ instead of $|=1 ?
Date: 05 Jul 2000 22:55:24 EDT
Organization: Abigail's Kinderboerderijen
Lines: 49
Message-ID: <slrn8m7uag.ibb.abigail@alexandra.delanet.com>
References: <8k0m4d$13o$1@panix3.panix.com> <slrn8m7qfq.8d6.tjla@thislove.dyndns.org>
Reply-To: abigail@delanet.com
NNTP-Posting-Host: 216.112.169.154
X-Date: MMDI September MCMXCIII
X-HTTP: http://purl.oclc.org/NET/Abigail
X-Revision: $Revision: 1.2 $
X-PGP-Fingerprint: 76 2E 82 7A 69 93 5F 97  AE 01 80 75 67 F3 45 D0
User-Agent: slrn/0.9.6.2 (Linux)
Xref: nntp.stanford.edu comp.lang.perl.misc:323646

Gwyn Judd (tjla@guvfybir.qlaqaf.bet) wrote on MMDI September MCMXCIII in
<URL:news:slrn8m7qfq.8d6.tjla@thislove.dyndns.org>:
'' I was shocked! How could kj0 <kj0@mailcity.com>
'' say such a terrible thing:
'' 
'' >Is there any reason why $|++ would be preferable over $|=1?
'' 
'' I can think of only one. Imagine doing this:
'' 
'' sub some_sub {
'' $|++;
'' ...
'' <code that assumes output is unbuffered>
'' ...
'' $|--;
'' }
'' 
'' sub some_other_sub {
'' $|++;
'' ...
'' <some more code assuming unbuffered output>
'' &some_sub;
'' ...
'' $|--;
'' # call some_sub again
'' &some_sub;
'' }
'' 
'' Doing it this way allows them each to fiddle with $| without needing to
'' worry about whether the calling sub expects $| to have a certain value
'' or not. ie. they set $| to the value it was before it was called. If
'' they did "$| = 1;" and "$| = 0" they could not guarantee this.

That's what local is for.  Your subs will fail to do the right thing
if $| happens to be -1. Or imagine that some_sub has a fatal error in
it, which is caught by an eval{}. Your entire $| ++ and $| -- will be
hosed.

And what is a sub that wants the output to *be* buffered going to do?

I'd say this is a good example showing how bad $| ++ actually is.


Abigail
-- 
BEGIN {$^H {join "" => ("a" .. "z") [8, 13, 19, 4, 6, 4, 17]} = sub
           {["", "Just ", "another ", "Perl ", "Hacker"] -> [shift]};
       $^H = hex join "" => reverse map {int ($_ / 2)} 0 .. 4}
print 1, 2, 3, 4, "\n";


the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu