Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This is because $thing could be defined but zero and we don't want to overwrite a perfectly valid zero from the command line.
As Not_a_Number points out, the syntax EXPR unless defined $thing will do nothing at all * if $thing is defined, whether it's true or false. That is, if we are executing EXPR, then $thing is guaranteed undefined, hence false; so $thing ||= 'default' is guaranteed to be the same as $thing = 'default'.

It seems reasonable to guess that what happened is that the coder originally had $thing ||= 'default' in some old code, discovered (as you mention) that it doesn't work when $thing is false-but-defined, and added the defined check without realising that it made ||= redundant.

Of course, a mere five years later, we have the wonderful //= (C style Logical Defined Or) instead to save us this pain.

UPDATE (the *'d statement above—sorry, I don't know how to do footnotes): On further thought, it's not quite true that EXPR unless defined $thing will do nothing if $thing is defined. Among what I suppose are many other subtle cases, if the unless is the last line in a subroutine, it'll make the subroutine return 1 when $thing is defined. For example, after

our $a = 1; sub b { 0 unless defined $a } sub c {} my $b = b; my $c = c;
we have that $b = 1 but $c is undefined.


In reply to Re^3: Parsing your script's command line by JadeNB
in thread Parsing your script's command line by grinder

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 perusing the Monastery: (6)
As of 2024-03-29 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found