|There's more than one way to do things|
Re: Re: Perl Idioms Explained - $|++by sauoq (Abbot)
|on Aug 01, 2003 at 21:26 UTC||Need Help??|
I would like to see this coded like this:
Ugh. I wouldn't. First, we don't need Yet-Another-Pair-Of-Constants-For-Zero-And-One. Really, most of us are pretty comfortable with the fact that 0 means 'false', 'no', or 'off' and that 1 means 'true', 'yes', or 'on' depending on context. Defining more aliases for every place where a zero or one will do is just false hubris.
Constants are good for two things. They are good when they can be used to replace a long or difficult-to-remember literal value. Something like use constant PI => 3.14159265358979; would fit in this category of use. The other time to use constants, as unintuitive as it seems, is when they might need to change. That is, when they might need to change between platforms, installations, or even executions but they need to remain constant throughout any single execution. Constants named things like MAXINT, INSTALL_BASE, and DEBUG probably fall in this category. Of course, the two categories aren't mutually exclusive. The LOCK_* constants provided by Fcntl might be an example of some that are good for both reasons.
This makes the meaning MUCH clearer.
Again, I disagree. It doesn't make the meaning clearer at all. The uninitiated user will still wonder what that $| variable is. That is the point that needs to be clarified not that a 1 means on and a 0 means off. Of course, you can make it clear with the line use English; which will provide you with the nicely named $OUTPUT_AUTOFLUSH alias.
Just the same, I'm completely comfortable with idiomatic perl and I don't use the English module. (If someone maintaining my code isn't already familiar with most special variables and doesn't at least know how to look them up in perlvar, then they're probably in over their heads anyway.) I use $| = 1; with abandon in smaller scripts¹.
Besides, even a nice alias for the admittedly esoteric $| doesn't address the larger problem that no one has yet mentioned. That is, $| is associated with the currently selected filehandle. Keeping track of that can be difficult. In larger scripts, I simultaneously handle that and the clarity issue by writing code like this
1. And yes, I'm guilty of using $|++ as well despite the fact that I agree it has drawbacks.
-sauoq "My two cents aren't worth a dime.";