in reply to Short form (ternary) if else

The ternary operator exists in many programming languages and is often a stumbling block for many who encounter it for the first time. I'm sure I scratched my head over it too. Here's an example of usage:

use strict; use warnings; my %hash = ( abc => 123 ); my $abc = exists $hash{abc} ? $hash{abc} : 0; my $xyz = exists $hash{xyz} ? $hash{xyz} : 0; print "ABC = $abc : XYZ = $xyz\n";

This outputs:

ABC = 123 : XYZ = 0

Another thing you need to know is that variables declared with my are lexical. With your example code, this means the first $vxdgs only exists in the if block and the second $vxdgs only exists in the else block; furthermore, they are two completely separate variables with their own values.

From your question, I believe the code you want is:

my $vxdg = exists $vxdgs{$node}{$disk} ? $vxdgs{$node}{$disk} : "";

-- Ken

Replies are listed 'Best First'.
Re^2: Short form (ternary) if else
by Bod (Parson) on Sep 09, 2023 at 22:27 UTC
    The ternary operator exists in many programming languages and is often a stumbling block for many who encounter it for the first time

    I must be decidedly odd - stop nodding at the back eyepopslikeamosquito - as I distinctly remember coming across the ternary operator in Perl and understanding it immediately. But at the same time thinking that I cannot see a use for it. Then, very soon afterwards I found a place where I could, and did use it...

    For a while I overused it in many places where if...else would have been more appropriate. I think I have the right balance between these options now.

Re^2: Short form (ternary) if else
by gg48gg (Sexton) on Feb 08, 2012 at 20:38 UTC
    Thanks all! I realize the scope issue. So, what I understand is that ternary form is only for use after an assignment (=) operator? Is that correct?

      That's not correct. The ternary operator will generate some sort of value - what you do with that value is up to you. You could, for example, use it to create a boolean return value:

      return defined $result ? 1 : 0;

      You can also nest ternary operators. Say you wanted to compare two numbers and produce: 1 if the first number was bigger; 0 if they were the same; -1 if the second number was bigger:

      my $cmp = $x > $y ? 1 : $x == $y ? 0 : -1;

      -- Ken

        This is the "spaceship" operator for comparing in a numeric sense:
        #!/usr/bin/perl -w use strict; my $x = 2; my $y = 3; my $cmp_result = $x <=> $y; print $cmp_result # -1
        Note: while naming variables the same as Perl key words is allowed (e.g. $cmp, (cmp is a key word), I personally try to avoid this).

        for comparison in a string context, use cmp:

        my $cmp_result = $stringX cmp $stringY;
        Update: As to the history, I have no idea why <=> is called the "spaceship" operator, but that is the common usage.

        Ken, I really do understand the scope thing. I just screwed it up when I first posted. But given the correct scope...

        Are you saying that the below code should work?
        my $vxdg; (exists $vxdgs{$node}{$disk}) ? $vxdg=$vxdgs{$node}{$disk} : $vxdg="";
        or will it only work like:
        my $vxdg = exists $vxdgs{$node}{$disk} ? vxdgs{$node}{$disk} : '';
        ok I think I get it. It returns a value if true, else return value if false. It does not execute code if true or code if false. Is that a correct statement?