user3689651
user3689651

Reputation: 69

How to execute multiple commands using ternary syntax in Perl?

How can I do something like the following:

(condition) ? (do this) (do this too) : (just do this then)

Upvotes: 3

Views: 2452

Answers (3)

mob
mob

Reputation: 118605

Do this and this too with do.

condition ? do { this(); this_too(); } : the_other_thing()

Upvotes: 10

ysth
ysth

Reputation: 98388

You want:

( (do this), (do this too) )

Upvotes: 2

terminarine
terminarine

Reputation: 1

A sample program that does that follows:

#!/usr/bin/perl

my $a = 1;

$result = ($a > 1) ? (print "\nDo this\n") ? (print "\nDo this also\n") ? "" : 
          (print "\nOr else do this\n");


$a = 6;

$result = ($a > 1) ? (print "\nDo this\n") ? (print "\nDo this also") ? "" : 
          (print "\nOr else do this\n");

Upvotes: -3

Related Questions