Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I made a Flashlight program.

A program? I thought you wanted to exercise OO? Object properties are usually stored in the object itself, so that you can create multiple instances. If in your program you find that you need another flash light, you'll have to duplicate your entire package, or at least modify it heavily, to support multiple on/off states.

I took the liberty of re-writing it, so you can OO the other machines yourself.

package Flashlight; use Carp; use Time::HiRes qw/usleep/; use strict; sub new { my ($class, %options) = @_; my $self = { }; my @required = qw/color/; my @optional = qw/batteries_included type battery_type/; for (@required) { croak "Required named argument $_ was not defined" if not defined $options{$_}; $self->{$_} = $options{$_}; } for (@optional) { $self->{$_} = $options{$_} if exists $options{$_}; } return bless $self, $class; } sub color { my ($self) = @_; return lc $self->{color}; } sub charge_batteries { my ($self, $type) = @_; carp 'Please read the manual!' if defined $type and $type ne $self->{battery_type}; return $self->{batteries_included} = 1; } sub is_on { my ($self) = @_; return $self->{on}; } sub is_off { my ($self) = @_; return ! $self->{off}; } sub on { my ($self) = @_; return undef if not $self->{batteries_included}; $self->{on} = 1; return return 1; } sub off { my ($self) = @_; return $self->{on} = 0; } sub switch { my ($self) = @_; return undef if $self->is_off and not $self->{batteries_included}; return $self->{on} = ! $self->{on} } sub flash { my ($self, $duration) = @_; croak 'Cannot flash while on' if $self->is_on; croak 'Cannot flash for a negative number of seconds' if $duration + < 0; carp 'flash() should be used to do nothing at all, changing durati +on' if defined $duration and $duration == 0; $duration ||= 0.1; $self->on(); usleep $duration; $self->off(); }

All those $self's and the new constructor indicate this flashlight is an object, and you can create as many flashlights as you want.

Now, it's your turn. I can say only one thing: try harder.

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk


In reply to Re: Re: Re: Beginner's Object Orientation Exercises? by Juerd
in thread Beginner's Object Orientation Exercises? by munchie

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found