Reputation: 2279
Recently I came across this Perl expression on a blog, unable to grab it purpose.
my $success = 1;
$success &&= $insert_handle->execute($first, $last, $department);
$success &&= $update_handle->execute($department);
Upvotes: 0
Views: 433
Reputation: 385809
EXPR1 &&= EXPR2;
is short for
EXPR1 = EXPR1 && EXPR2;
(Except that EXPR1
is only evaluated once.*)
The provided code
my $success = 1;
$success &&= $insert_handle->execute($first, $last, $department);
$success &&= $update_handle->execute($department);
could also have been written as:
my $success = 0;
if ($insert_handle->execute($first, $last, $department)) {
if ($update_handle->execute($department)) {
$success = 1;
}
}
or
my $success = $insert_handle->execute($first, $last, $department)
&& $update_handle->execute($department);
* — This matters if EXPR1
has side-effects, which could very well happen if it's a magical variable or an lvalue sub.
my $x = 3; # Prints:
print("$x\n"); # 3
sub x() :lvalue { print("x\n"); $x }
x &&= 4; # x
print("$x\n"); # 4
x = x && 5; # x x
print("$x\n"); # 5
Upvotes: 9
Reputation: 143091
It is a long way of saying.
my $success =
$insert_handle->execute($first, $last, $department)
&& $update_handle->execute($department);
Upvotes: 3
Reputation: 213253
Might be : -
my $success = 1;
$success = $success && $insert_handle->execute($first, $last, $department);
$success = $success && $update_handle->execute($department);
Similar to : -
a += b
// is equivalent to
a = a + b
Upvotes: 1