Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you store a result to a variable and check the result in a conditional?

Tags:

perl

perl5.8

I know it's possible but I'm drawing a blank on the syntax. How do you do something similar to the following as a conditional. 5.8, so no switch option:

while ( calculate_result() != 1 ) {
    my $result = calculate_result();
    print "Result is $result\n";
}

And just something similar to:

while ( my $result = calculate_result() != 1 ) {
    print "Result is $result\n";
}
like image 662
Oesor Avatar asked Apr 22 '10 19:04

Oesor


1 Answers

You need to add parentheses to specify precedence as != has higher priority than =:

while ( (my $result = calculate_result()) != 1 ) {
    print "Result is $result\n";
}
like image 53
Matteo Riva Avatar answered Sep 20 '22 21:09

Matteo Riva