Just another Perl shrine | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
I've never used the bit wise version yet in Perl. Probably because I seldom use Perl for any bit manipulation, but I have twiggled bits although that is not Perl's forte!
This ||= critter can show up in a number of contexts. For me the most common ones are:
1). Default after regex fails 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: 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. In reply to Re^3: What is the difference between |= and ||=?
by Marshall
|
|