Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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

use IO::Handle; STDOUT->autoflush;

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

In reply to Re: Re: Perl Idioms Explained - $|++ by sauoq
in thread Perl Idioms Explained - $|++ by broquaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found