-3

I need your help for this line I can't understand the meaning of this check

if (!$value) {$value = 0;}
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 2
    See the [`perlop`](https://perldoc.perl.org/perlop.html) reference - this covers Operators and the like, including `!` under Symbolic Unary Operators. Likewise `perlsyn` and related are also relevant reads.. – user2864740 Feb 06 '19 at 18:00
  • 1
    @toolic, That's one of the worse written parts of the docs. See my comments in the answers of [this question](https://stackoverflow.com/q/1036347/589924) for why. See the [second answer](https://stackoverflow.com/a/5655485/589924) for much better documentation. – ikegami Feb 06 '19 at 18:07
  • (It's an incomplete list of false values with an arbitrary of list false-returning code snippets that contains duplicates mixed in.) – ikegami Feb 06 '19 at 18:32
  • 1
    @ikegami documentation patches are always appreciated by the porters, provided it still manages to convey the information the user needs. – Grinnz Feb 06 '19 at 21:47

1 Answers1

4

It is the unary negation operator.

  • If the value of $value is true, !$value will evaluate to a false value.

  • If the value of $value is false, !$value will evaluate to a true value.

As a whole, the statement will set $value to 0 if $value was false.

It could have been written as follows:

$value ||= 0;

It is surely being used a shortcut for the following:

if (!defined($value)) { $value = 0; }    # Can be written as: $value //= 0;
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • hello, I just need to confirm what I got from you right or not is that meaning it's checking about if check if the variable is empty or not and if it is empty it will set the value of the variable equal to 0 is that right? – Mahmoud Shater Feb 06 '19 at 18:14
  • Assuming you mean "undefined" by "empty", then not quite. While undefined is false, that's not the only false value. – ikegami Feb 06 '19 at 18:16
  • It will set the variable to 0 if $value does not evaluate to true. That is the case, for 0, the empty string, undefined, an empty list, and the false value returned by !$anything itself. – bytepusher Feb 06 '19 at 19:55
  • I don't think it can be considered a shortcut for "not defined", as stated in the last point. Rather, it normalizes all false values (numerical 0, the string "0", empty string and undefined) into a single kind of false value (0). – Silvar Feb 06 '19 at 22:02
  • @Silvar, That's extremely unlikely. The code in question probably only accepts an optional number that defaults to zero. One could use `!defined($value)` to check for that, but `!$value` is a perfectly valid shortcut the code appears to have used. – ikegami Feb 06 '19 at 23:25
  • I don't think it's worth assuming the intent of the code given a single out of context line. I have written a lot of code that checks for false, and would break if you checked for undef instead. – Grinnz Feb 06 '19 at 23:51
  • @Grinnz, Quite so, but 1) I said the code is checking for false in lieu of undef (not checking for undef in lieu of false), and 2) I never said they were equivalent. I simply explained what `if (!$value) {$value = 0;}` is doing. – ikegami Feb 07 '19 at 00:23