The nice thing about short-circuiting

I just noticed in my own code that even though I am comfortable with this expression:

$a //= 123;

when the right-hand side is some function call, especially one with side-effects or big performance penalty, I tend to write:

unless (defined $a) { $a = foo() }

which is silly because // and //= short-circuits. The same case for || and ||=, et al. It's just irrational fear.

2 Comments

I've recently found myself using this nice short-circuiting construct quite a bit...

state $foo = do {
  # some complex calculation
};

Leave a comment

About Steven Haryanto

user-pic A programmer (mostly Perl 5 nowadays). My CPAN ID: SHARYANTO. I'm sedusedan on perlmonks. My twitter is stevenharyanto (but I don't tweet much). Follow me on github: sharyanto.