I made a Flashlight program.
#! Perl
use vars qw($on $pwrlvl);
use strict;
my $on = "no";
my $pwrlvl = "3";
my $fl = "Flashlight";
print "You are holding a flashlight. What will you do? \n";
print "(TurnOn, TurnOff, Flash, ChangePowerLevel, ExitProgram)\n";
print "/Status Checks/=\\ pwrlvl, ison \\\n";
while(<STDIN>) {
chomp;
if (/^Change/) {
print "Enter Level: ";
chomp (my $lvl=<STDIN>);
print $fl->ChangePowerLevel("$lvl");
print "\n";
} elsif (/^(exit|q|end|x|quit)/i) {
exit;
} else {
print $fl->$_ ."\n";
}
}
{ #1
package Flashlight;
sub TurnOn {
$on = "yes";
return "The light is on.";
}
sub TurnOff {
$on = "no";
return "Light is now off.";
}
sub Flash {
if ($on eq "no") {
return "A flash of light beams through the air!";
} else {
return "The flashlight is on already.";
}
}
sub ChangePowerLevel {
my $class = shift;
my $level = shift;
$pwrlvl = $level;
return "The Power level is set at $pwrlvl";
}
sub pwrlvl {
return "Power is at $pwrlvl";
}
sub ison {
return "Is it on? Well, $on";
}
}
It has everything but the battery power, which will be easy to implement in a few minutes when I find time.
Commands: ison, pwrlvl, ChangePowerLevel, TurnOn, TurnOff, Flash
> munchie, the number munchin newb
Llama: The other other white meat!
(you had to be there :-P)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|