Gossamer Forum
Home : General : Perl Programming :

Ternary Conditional Operator

Quote Reply
Ternary Conditional Operator
I use the ?: conditional operator alot in C/C++ and I was wondering if anyone uses it in Perl? I see alot of programs that use the traditional if...else. But, I'm fairly new to Perl so I haven't looked at alot of code. Just curious because Ternary Conditional Operators are usually more effient than their counterpart.

Thanks,
Paul

http://www.fullmoonshining.com for Pearl Jam Fans
Quote Reply
Re: Ternary Conditional Operator In reply to
Yes...you can use that in Perl....

Like the following:

Code:

$Something ?
print qq|PRINT SOME HTML|:
print qq|SOME OTHER HTML|;



Regards,

Eliot Lee
Quote Reply
Re: Ternary Conditional Operator In reply to
Thanks, yeah I knew it was present, I was curious if it was readily used.
I use it in this fashion:
c = a < b ? a : b;

Thanks again,
Paul

http://www.fullmoonshining.com for Pearl Jam Fans
Quote Reply
Re: Ternary Conditional Operator In reply to
I know that Gossamer Threads has used it a lot in their scripts and some perl programmers, like jerrysu, have used it in their modifications.

Wide spread? Probably not. Using "conditional" if/else/elsif statements are easier to understand and most application developers tend to program scripts that will be easily understand by their users.

Which is better? Not quite sure...may be Mark or Alex can comment on this.

Regards,

Eliot Lee
Quote Reply
Re: Ternary Conditional Operator In reply to
On a simple if..else, yes I use it, and quite often. Smile

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: Ternary Conditional Operator In reply to
You should always use ternary over if/else when you can.
It makes for much cleaner and smaller code but more importantly, it makes for a faster execution.

Code:
Benchmark: timing 1000000 iterations of ifelse, tern...
ifelse: 3 wallclock secs ( 2.31 usr + 0.00 sys = 2.31 CPU) @ 432900.43/s (n=1000000)
tern: 1 wallclock secs ( 1.38 usr + 0.00 sys = 1.38 CPU) @ 724637.68/s (n=1000000)
-g


s/(\d{2})/chr($1)/ge + print if $_ = '8284703280698276687967';
Quote Reply
Re: Ternary Conditional Operator In reply to
Sorry, GClemmons...I meant to include you in the list above as someone who would be able to properly address this problem.

Thousand pardons....Wink

Regards,

Eliot Lee
Quote Reply
Re: Ternary Conditional Operator In reply to
Thanks everyone. GClemmons thanks for the benchmark. Yeah, it looks like it is executes alot like it does in C/C++ in terms of raw speed. Thanks Again,
Paul

http://www.fullmoonshining.com for Pearl Jam Fans