Normally in this situation, you want to choose between 3 choices, not two!
Use the ternary operator to set a variable dependent upon a true/ false condition.
A common use would be like this:
($today eq 'Monday') ? $firstDay =1 : $firstDay =0;
That would be preferred over:
$firstDay=0;
$firstDay=1 if ($today eq 'Monday');
Use if and if/elsif/else conditions for more complicated things.
#!/usr/bin/perl -w use strict; decide ('add'); decide ('edit'); decide ('bad input nothing'); print "\n"; decide2 ('add'); decide2 ('edit'); decide2 ('bad input nothing'); sub decide { my $input = shift; print "invalid input\n" if ($input ne "add" and $input ne "edit"); print "doing an add\n" if $input eq "add"; print "doing an edit\n" if $input eq "edit"; } sub decide2 { my $input = shift; if ($input eq 'add') { print "the add code goes here\n"; } elsif ($input eq 'edit') { print "the edit code goes here\n"; } else { print "the invalid input code goes here\n"; } } __END__ doing an add doing an edit invalid input the add code goes here the edit code goes here the invalid input code goes here
In reply to Re: true/false condition - what am I doing wrong?
by Marshall
in thread true/false condition - what am I doing wrong?
by ultranerds
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |