http://www.perlmonks.org?node_id=975572

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I was using the short c-style IF statment in my code and I understood it worked by EXPR ? TRUE :FALSE; however when I compiled this:

 6 < 8 ? $ans = "true" : $ans = "false";

$ans was false... can anyone explain what happened?

Replies are listed 'Best First'.
Re: if statement confusion
by toolic (Bishop) on Jun 11, 2012 at 14:15 UTC
Re: if statement confusion
by Anonymous Monk on Jun 11, 2012 at 14:13 UTC

    Operator precedence. What you got was: (6 < 8 ? $ans = "true" : $ans) = "false";

Re: if statement confusion
by davido (Cardinal) on Jun 11, 2012 at 17:31 UTC

    Write it like this:

    $ans = 6 < 8 ? 'true' : 'false';

    update: Aack! I didn't notice that toolic had already given this suggestion. Sorry for creating an echo in the Monastery. :)


    Dave

Re: if statement confusion
by Anonymous Monk on Jun 11, 2012 at 14:56 UTC

    Thank you :)

Re: if statement confusion
by eyepopslikeamosquito (Chancellor) on Jun 12, 2012 at 12:28 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.