5 Replies - 3334 Views - Last Post: 26 June 2012 - 07:33 AM

#1 MartinKravec  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 20-June 12

Is this correct conditional expression ? [PERL]

Posted 20 June 2012 - 06:23 AM

Is this correct conditional expression ? [PERL]
$graph->$param->{a} = $a or 0;


I want to set $a if is defined, otherwise 0.
Is This A Good Question/Topic? 0
  • +

Replies To: Is this correct conditional expression ? [PERL]

#2 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Is this correct conditional expression ? [PERL]

Posted 20 June 2012 - 10:16 PM

No, Perl doesn't support or statement like the RDMS-es. It gives us an alias, the || operator. You can use it to test a variable against its current value:

$a ||= 0;



Of course it depends on what you mean by defined. In Perl, 3 defined variables are still treated as if they are undefined: numeric 0, string "0" and a blank string "". So if you define a variable to any of these values, technically you have defined the variable to some value, but still Perl will return False for the || test. So if you want to use defined in the truest sense of the term, that is whether the variable hasn't been defined at all, or defined and initialized to a null value, you should use the defined function:

$a = defined($a) ? $a : 0;



So for your issue it will be

$graph->$param->{a} = $a || 0; // or $graph->$param->{a} = defined($a) ? $a : 0;


This post has been edited by cupidvogel: 20 June 2012 - 10:32 PM

Was This Post Helpful? 2
  • +
  • -

#3 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Is this correct conditional expression ? [PERL]

Posted 25 June 2012 - 04:59 AM

View Postcupidvogel, on 21 June 2012 - 06:16 AM, said:

No, Perl doesn't support or statement like the RDMS-es. It gives us an alias, the || operator.


Not true. Perl has both the || and the or operators. The difference between them is one of operator precedence; || has very high precedence, while or has very low precedence. The OP's posted code doesn't work because or has a lower precedence than =, causing the expression to be parsed as ($graph->$param->{a} = $a) or 0; rather than the intended $graph->$param->{a} = ($a or 0);. If you add the parentheses explicitly, it will work.

View Postcupidvogel, on 21 June 2012 - 06:16 AM, said:

In Perl, 3 defined variables are still treated as if they are undefined: numeric 0, string "0" and a blank string "".


Well... not really... Those values aren't "treated as if they are undefined", those values are simply false and or/|| tests for truth, not definedness. The practical upshot of this (as well as the solution to it) are still the same as what you said, but truthiness and definedness are distinct concepts in Perl and it's often important to keep that distinction clear.

One other option for correctly testing definedness in this case is the defined-or operator, //, which was added in Perl 5.10:
$graph->$param->{a} = $a // 0;

Was This Post Helpful? 2
  • +
  • -

#4 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Is this correct conditional expression ? [PERL]

Posted 25 June 2012 - 05:06 AM

Yeah yeah sorry, I messed up in both even after knowing them. The or one I have almost never used, so I forgot that it even exists, but the definedness stuff was definitely a typo... :whistling: Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Is this correct conditional expression ? [PERL]

Posted 26 June 2012 - 06:42 AM

View Postcupidvogel, on 25 June 2012 - 01:06 PM, said:

The or one I have almost never used, so I forgot that it even exists,


It's not normally used in this context because of the operator precedence issue, yeah. I tend to stick to the rule of using && and || only for boolean logic ($foo = $bar || 0, if ($foo && $bar)) and using and and or only for flow control (open my $fh, ',', $filename or die, $x = get_number() and print 10 / $x).
Was This Post Helpful? 0
  • +
  • -

#6 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Is this correct conditional expression ? [PERL]

Posted 26 June 2012 - 07:33 AM

Right.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1