Re: What is the difference between |= and ||=?
by gwadej (Chaplain) on Apr 07, 2009 at 13:35 UTC
|
The fact that |= is a bitwise or and ||= is a logical or has a few consequences not shown in your code above. For example,
$var = 2;
$var ||= 1; # $var now has the value 2.
$var |= 1; # $var now has the value 3.
One non-obvious effect that is worth mentioning is shortcut behavior. The right hand side of ||= is not evaluated if the left hand side is true. But, both sides of |= are always evaluated.
Update: Corrected term bitwise
| [reply] [d/l] [select] |
Re: What is the difference between |= and ||=?
by Anonymous Monk on Apr 07, 2009 at 13:26 UTC
|
perlop explains that || is "logical or" while | is "Bitwise Or" | [reply] |
Re: What is the difference between |= and ||=?
by jettero (Monsignor) on Apr 07, 2009 at 13:26 UTC
|
One is bitwise and one is logical. See perlop for further details.
| [reply] |
Re: What is the difference between |= and ||=?
by ikegami (Patriarch) on Apr 07, 2009 at 14:19 UTC
|
The following sets the var to 1 too, and it even looks similar, so it must be the same operator as the other two, right?
my $var3 = 1;
Why does that bizarre logic keep surfacing? Operators are documented in perlop.
In fact, you should be using my $var = 1; in this case. I'm surprised no one has mentioned that it makes no sense for my $var to be the LHS of |= or ||=.
my $var |= 1;
my $var2 ||= 1;
means
my $var;
$var = $var | 1;
^^^^
Known to be undef since it was just created.
my $var2;
$var2 = $var2 || 1;
^^^^^
Known to be undef since it was just created.
| [reply] [d/l] [select] |
|
It's much clearer now, thanks :)
I get warnings with hash references and I've used code such as to get around the problem:
$some_href->{id} ||= 0;
Is that a good style? If not, what are the alternatives? | [reply] [d/l] |
|
Assuming the empty string isn't a valid id, that will convert undef to zero.
Whether it makes sense or not can only be derived from context, but it seems to me that you might be silencing the symptom, not fixing the problem. Why is id undef in the first place? Why would it makes sense to set it to zero when it is?
| [reply] [d/l] |
|
|
|
|
$some_href->{id} ||= 0;
Is that a good style?
Whether or not it's good style depends on what you intend to do.
The statement $some_href->{id} ||= 0; will assign 0 to the hash element if the hash element is anything that Perl evaluates as false, e.g., undef, 0, '0' or '' (the empty string).
The statement could be used to 'canonicalize' any false value to the numeric value of 0. If this is the intent of the statement, it is good style.
| [reply] [d/l] [select] |
|
my $license_class = ($raw_html =~ m|Class:</td><td class="di">(\w+?)
+|)[0];
$license_class ||= "BAD_CLASS";
The above is a "web HTML" scraper app. One common paradigm would be to assign a default and then call something that will override that default if something is found. Here, the regex will always return something (and undef IS "something"), so I just apply the default as the second step.2). Use ||= for managing a cache of prior results:
...in some sub....
return ( $hash{$value} ||= expensive_func($value) );
Here I am automatically caching the results of some expensive_func. This the same as $hash{$value} = $hash{$value} || expensive_func($value);
Perl is magic in that an undefined value in a hash, like $some_href->{'id'} will evaluate to "false" in an "if" statement. You will get an error if this expression is used in say a print or some math operation and it is undefined. Whether you are in case (1) above and know exactly what is gonna happen is something that you have to decide. But, yes using ||= to set a default value is fine style. | [reply] [d/l] [select] |
|
Please consider using the cleaner perl5.10 variant //= if you expect $some_ref->{id} to be nonexistent / not defined.
$some_href->{id} //= 0
This makes it explicit that you only want to set id when it was not defined before. Then the truthy, falsy nature of '', 0 and other variants is no longer of concern.
print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
| [reply] [d/l] [select] |
|
|
Re: What is the difference between |= and ||=?
by lostjimmy (Chaplain) on Apr 07, 2009 at 13:51 UTC
|
| [reply] |
Re: What is the difference between |= and ||=?
by JavaFan (Canon) on Apr 07, 2009 at 13:33 UTC
|
my $var3 += 1;
my $var4 .= 1;
Now $var3 and $var4 are all set to the value 1 as well. | [reply] [d/l] |