The $hash{key} = $value || 0 construct avoids a value of undefined. It is like setting a default value, or if you are familiar with SQl, like the NVL() function. If you are going to test the value of a variable later in the program, and the value might be undefined, this is used to avoid "Use of uninitialized value" warnings.
use strict; use warnings; my (%hash, $value); print "Without\n"; $hash{key} = $value; if ($hash{key} == 42) { print "taking appropriate action\n" } print "With\n"; $hash{key} = $value || 0; if ($hash{key} == 42) { print "taking appropriate action\n" }
Output:
/wlsedi/howard$: perl pm.pl Without Use of uninitialized value in numeric eq (==) at pm.pl line 10. With /wlsedi/howard$:
In reply to Re: query on hash understanding
by GotToBTru
in thread query on hash understanding
by Ritz@Perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |