hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show you something first before i go to my delimma.

codes as follows using Mrs. If Else:

#!perl/bin/perl use strict; print "Are you sure you want to quit ('yes' or any key for 'no'): "; chomp($_ = <STDIN>); if (/\byes\b/i) { print "Press any key to exit..."; <STDIN>; exit; } else { print "Thanks, you stayed... But then, goodbye.\n"; print "Press any key to proceed...\n"; <STDIN>; } print "GOOD DAY";

equivalent codes for Mr. Ternary as follows:

#!perl/bin/perl use strict; print "Are you sure you want to quit ('yes' or any key for 'no'): "; chomp($_ = <STDIN>); /\byes\b/i ? eval { print "Press any key to exit..."; <STDIN>; exit; } : eval { print "Thanks, you stayed... But then, goodbye.\n"; print "Press any key to proceed...\n"; <STDIN>; }; print "GOOD DAY";

Advantages of Mr. Ternary over Mrs. If Else:

1. shorter code

2. permits the parenthesis not to be used in the expression

3. permits the curly braces not to be used (well in here, eval takes its place. and note if you put an eval over Mrs. If Else, then Mr. Ternary is more appropriate)

4. promotes runtime error handling (the program doesn't exit so sudden)

5. the statements are more clearer to understand after a true or false evaluation

Disadvantages of Mr. Ternary:

1. difficulty in reading (only for the first timers because this is not conventional)

2. the return value of eval cannot be assigned to another variable (but there is the special variable $@ that holds it for you. you can evaluate that after the ternary operation)

3. tedious in bullet proofing a program (it is because you cannot quickly notice the runtime error inputted by the end user or file. in which Mr. Ternary uses the eval. it happens when your statements are so lenghty inside the eval block. but here is a solution for that, see next)

optional code revisions while bullet proofing:

#!perl/bin/perl use strict; print "Are you sure you want to quit ('yes' or any key for 'no'): "; chomp($_ = <STDIN>); /\byes\b/i ? \& { print "Press any key to exit..."; <STDIN>; exit; } : \& { print "Thanks, you stayed... But then, goodbye.\n"; print "Press any key to proceed...\n"; <STDIN>; }; print "GOOD DAY";

the only code was changed in here was "eval" to "\&"... now my delimma goes here... i don't know whats "\&" for. i just seen it on the Perl documentation under the strict pragma, and i read what it says "There is one exception to this rule...\& is allowed so that goto &$AUTOLOAD would not break under stricture.". well im just a novice programmer with Perl i don't really understand that. so what does "\&" mean?

back to Mr. Ternary, i think there is just only one slight drawback in using the ternary operator. that is difficulty in reading. but then its easy to overcome. so in other words Mr. Ternary is more handy than Mrs. If Else. so i think Mrs. If Else should be dropped from our system.

but then, if you have other downs in mind on Mr. Ternary, your statements will be gladly evaluated, then i will give you the output.

thanks in advance... keep deep and dark!

From: PerlPhi


In reply to Mr. Ternary is greater than Mrs. If Else by PerlPhi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.