Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hmmmm, nop is right ... that is not really OO. You are using packages alright, but not in an OO manner. First, you need a constructor to create the object:
package Flashlight; sub new { my $class = shift; my $power = shift || 0; my $level = shift || 0; my $self = { level => $level, power => $power, }; return bless $self, $class; }
This allows you to set a level and whether it is on or off when you instantiate (create) the object:
my $light = Flashlight->new(1,4);
which will create a flashlight that is on(1) with a level of 4.

Next, you really shouldn't print messages to STDOUT inside an object's method(s). Instead, methods really should generally only get and set values. This means that your methods Flash and ChangePowerLevel really belong in the client that uses your Flashlight object. Also, pwrlvl and ison should return true or false values, and should be renamed to more standard names like change_level and is_on.

Here is a complete re-write of your good but flawed first attempt (don't fret it, OO is an art form that takes a looong time to 'get'). Like music, this is just my interpretation of the problem at hand.

use strict; my $light = Flashlight->new(); my %menu = ( 1 => ['Turn On', sub { $light->turn_on }], 2 => ['Turn Off', sub { $light->turn_off }], 3 => ['Toggle', sub { $light->toggle }], 4 => ['Flash', sub { print flash($light),"\n" }], 5 => ['Change Level', sub { change_level($light) }], 6 => ['Status', sub { print $light->to_string }], 7 => ['Exit', sub { exit }], ); while(1) { print menu(); chomp(my $choice = <>); last if $choice =~ /^(exit|q|end|x|quit)/i; next unless exists $menu{$choice}; &{$menu{$choice}->[1]}; } sub menu { my $string; for (sort keys %menu) { $string .= sprintf ("%d) %s\n",$_,$menu{$_}->[0]); } return $string . 'Enter choice:'; } sub flash { my $ref = shift; return $ref->is_on ? "The flashlight is on already." : "A flash of light beams through the air!" } sub change_level { my $ref = shift; print "Enter Level: "; chomp (my $lvl=<>); $ref->level($lvl); print "Power is at ", $ref->level, "\n"; } ############################################### package Flashlight; # constructor sub new { my $class = shift; my $power = shift || 0; my $level = shift || 0; my $self = { level => $level, power => $power, }; return bless $self, $class; } # set method sub turn_on { my $self = shift; $self->{'power'} = 1; } # set method sub turn_off { my $self = shift; $self->{'power'} = 0; } # set method sub toggle { my $self = shift; $self->{'power'} = ! $self->{'power'}; } # get or set method sub level { my $self = shift; my $level = shift; return $self->{'level'} unless $level; $self->{'level'} = $level; } # get method sub is_on { my $self = shift; return $self->{'power'}; } # get method sub to_string { my $self = shift; my $string; while (my($k,$v) = each %$self) { $string .= "$k => " . ($v || 0) . "\n"; } return $string; }


(the triplet paradiddle with high-hat)

In reply to (jeffa) 3Re: Beginner's Object Orientation Exercises? by jeffa
in thread Beginner's Object Orientation Exercises? by munchie

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2023-03-22 19:50 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.