codes as follows using Mrs. If Else:
Expand|Select|Wrap|Line Numbers
- #!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";
Expand|Select|Wrap|Line Numbers
- #!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";
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:
Expand|Select|Wrap|Line Numbers
- #!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";
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