Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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.

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 a soft breeze sighs...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2018-05-25 17:48 GMT
Find Nodes?
    Voting Booth?