Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Parsing your script's command line

by Not_a_Number (Parson)
on Aug 14, 2003 at 18:07 UTC ( #283951=note: print w/ replies, xml ) Need Help??


in reply to Parsing your script's command line

Very nice, ++

However, I have a small question on this snippet:

my $thing = shift; $thing ||= 'default' unless defined $thing;

Surely the || is redundant here?

my $thing = shift; $thing = 'default' unless defined $thing;

This seems to do the job just as well?

Or am I missing something?

thx

dave


Comment on Re: Parsing your script's command line
Select or Download Code
Re: Re: Parsing your script's command line
by yosefm (Friar) on Sep 04, 2003 at 11:48 UTC
    This is because $thing could be defined but zero and we don't want to overwrite a perfectly valid zero from the command line.

      Er...

      I'm afraid I don't understand. Under what circumstances does:

      $thing = 'default' unless defined $thing;

      not work for $thing == 0? I'm confused. Could you show me some code where the behaviour of the above differs from:

      $thing ||= 'default' unless defined $thing;

      Thanks in advance,

      dave

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://283951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2015-07-02 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (27 votes), past polls