Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The recent Chatterbox bug discussion reminded me of a huge pet peeve of mine about Perl (and hugely peevish too, I know): it’s such a pain to test string length if you want to accept undefined values without emitting warnings. You have to write something like this:

$foo if defined $str and length $str;

It’s so redundant and verbose that people will often simply do

$foo if $str;

and accept that a value of 0 or "0" will produce false negatives with a shrug.

In other words, the easy and obvious way is wrong.

That could have been avoided so simply:

BEGIN { *CORE::GLOBAL::length = sub(;$) { defined $_[0] ? CORE::length( $_[0] ) : undef; }; }

The above test for string length then becomes this:

$foo if length $str;

Since undef silently evaluates to false in boolean context, same as 0, this never throws an uninitialized warning and does the right thing. This version of length is also more useful, since you can tell undefs from empty strings just by looking at the return value, without an extra defined test. Granted, now you have to check the return value for definedness if you want to use it in calculations, but the situation does not actually change, since you need a definedness test in either case – either on the passed scalar or on length’s return value.

I’ve been using this in some scripts for a while, and I love it. I can’t think of a single reason why anyone would want to have length’s current behaviour of returning 0 while throwing a warning on undef scalars, as opposed to returning undef without a complaint. The latter seems more useful and/or less annoying in every case I can come up with.

Of course, it’s way too late to do anything about this now. I daren’t use it in any complex codebase that relies on a lot of CPAN code either. Maybe something to be dealt with using feature?

Oh well.

Makeshifts last the longest.


In reply to This could have DWIM better by Aristotle

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 surveying the Monastery: (5)
As of 2024-03-28 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found