Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Understanding the definition of 'short-circuiting'

1 view
Skip to first unread message

Steve Bertrand

unread,
Dec 29, 2009, 9:27:16 PM12/29/09
to Perl Beginners
Happy holidays everyone!

I've found Devel::Cover to be an exceptionally handy item, but don't
think I entirely grasp what the term 'short-circuiting' actually means.

I hope this is a beginner-type question for beginners who are writing
tests ;)

My code (snip, for context):

if ( ! $self->IN_TEST_MODE() && $self->ENABLE_REPLICATION() ) {

my $schema
= ISP::RADIUS::Storage::Replicated->connect( @{ $master } );

$schema->storage->connect_replicants( @{ $database_servers } );

return $schema;
}

my $schema
= ISP::RADIUS::Storage->connect( @{ $master } );

#... return a single storage schema later if condition fails

Devel::Cover, when running its cover utility as `cover -test` reports
this as my condition:

"not $self->IN_TEST_MODE and $self->ENABLE_REPLICATION", which I understand.

What I don't grasp, is how this passes the "short-circuit" tests, but
fails the other two tests.

I know what I need to do to ensure that the condition has test coverage,
but I'm confused as to what "short-circuit" is.

Steve

Robert Wohlfarth

unread,
Dec 29, 2009, 9:52:28 PM12/29/09
to Perl Beginners
On Tue, Dec 29, 2009 at 8:27 PM, Steve Bertrand <st...@ibctech.ca> wrote:

> Happy holidays everyone!
>
> I've found Devel::Cover to be an exceptionally handy item, but don't
> think I entirely grasp what the term 'short-circuiting' actually means.
>

I'm not familiar with Devel::Cover. The phrase "short circuit" usually
refers to the boolean operators && and ||. Perl stops evaluating boolean
operations as soon as it can. For example, take this code:
if ($true || $false) { print "True\n"; }

The || operator evaluates as true whenever at least one condition evaluates
true. Perl grabs the left hand side: $true. It sees a true value and prints
the line. Perl never looks at $false because the value of $false doesn't
matter.

Now consider this example:
if (do_first(1, 2) || do_second(\$result)) { print "True\n"; }

Assume do_first returns true and the line prints. What about $result? Perl
never called into the code for do_second, and $result never changed. We say
that Perl short circuited the evaluation. It stopped executing code as soon
as it knew the outcome of the logic statement.

--
Robert Wohlfarth

0 new messages