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:
[CODE=perl]#!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";[/CODE]
equivalent codes for Mr. Ternary as follows:
[CODE=perl]#!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";[/CODE]
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:
[CODE=perl]#!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";[/CODE]
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
					codes as follows using Mrs. If Else:
[CODE=perl]#!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";[/CODE]
equivalent codes for Mr. Ternary as follows:
[CODE=perl]#!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";[/CODE]
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:
[CODE=perl]#!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";[/CODE]
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
Comment