Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Short form (ternary) if else

by Riales (Hermit)
on Feb 08, 2012 at 19:59 UTC ( #952570=note: print w/ replies, xml ) Need Help??

in reply to Short form (ternary) if else

I think what you're looking for is this:

my $vxdg = exists $vxdg{$node}{$disk} ? $vxdg{$node}{$disk} : '';
Also, in your if/else statement, if you declare $vxdg inside the if/else like you are doing, it goes out of scope by the time you get out of the if/else block.

Comment on Re: Short form (ternary) if else
Select or Download Code
Replies are listed 'Best First'.
Re^2: Short form (ternary) if else
by gg48gg (Sexton) on Feb 08, 2012 at 20:29 UTC
    Yes, that works beautifully! Thank you! I realize the scope issue as well. So, what I understand is that ternary form is only for use after an assignment (=) operator? Is that correct?

      No, it doesn't have to be used in conjunction with assignment. Consider:

      printf("X is %s\n", defined $X ? $X : 'undefined');


      sub get_name { # ... return wantarray ? ($firstname, $middlename, $lastname) : "$firstname $lastname"; }

      Yes and no... It is typically at it's most useful after an assignment, and it's use is that it generates a value, but that doesn't necessarily mean it needs to be used in an assignment. Others have given examples, but the essential thing to remember is that the ternary operator returns a value. if does not: It is a flow-control operator.

      So, where it does need to be used is where Perl would expect a value. Assignments are one common case of that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://952570]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2015-10-09 13:14 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (242 votes), past polls