[Image of Linux]
Linux's Witness Ministry
The Personal Web Pages of Chris X. Edwards

ex-ifunless.pl

--------------------------
#!/usr/bin/perl -w
#ex-ifunless.pl
#

#normal if statement (note "eq" for text comparison)
$x="ok";
if ($x eq "ok") {
  print "everything's fine \n";
}

#if with else clause
srand(time); $x=rand(10); #pick a random number 0-10
if ($x < 5) {
print $x," is TOO small \n";
}
else {
print $x," is TOO large \n";
}

#if with else and elsif (unlimited elsifs) clauses
$x=rand(10);
if ($x < 5) {
print $x," is TOO small \n";
}
elsif ($x > 6) {
print $x," is TOO large \n";
}
else {
print $x," is TOO incredibly average \n";
}

#normal unless statement 
$x="evil";
unless ($x eq "evil") { #same as: if (not $x eq "ok")
  print "everything's fine \n";
}
else {print "something is wrong \n"; #else statement optional also
}

--------------------------
Return to Program Examples
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
Chris X. Edwards ~ January 2003