Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
my $auto = new car("Opel Vectra",50000);
Do:
my $auto = car->new("Opel Vectra",50000);
Create a container class that provides methods meant for groups of cars. Add a license plate (UUID) field to the Car class. You can even support sub classes of Car (package names should start with an upper case letter) using polymorphism.

Code not tested or fully complete but you get the gist. Note: it requires you pass it Car instances. You could get fancy and roll that up in it, but I wouldn't personally.

use strict; use warnings; package Garage; sub new { my $pkg = shift; my $self = { all_cars => {}, # store cars key'd by license plate (uuid) } blese $self, $pkg; return $self; } sub add_car { my ($self, $car) = $@; #... add car key'ed by license plate or uuid } sub del_car { my ($self, $uuid) = $@ delete $self->get_cars->{$uuid}; } sub get_cars { my $self = shift; return $self->{all_cars}; } sub get_car { my ($self, $uuid) = $@; my $cars = $self->get_cars; die "Car with plate $uuid is not in the garage!\n\n" if not $cars->{ +$uuid}; # die can throw a reference also, so there you can do the exc +eption thing in the caller return $cars->{$uuid}; } sub count_cars { my $self = shift; my $cars_ref = $self->get_cars; my $count = @{keys %$cars_ref}; return $count; } # add other "collective" methods 1;
PS: nice you're not using an OOP framework, because you don't need one xD

PPS: So I suggested the opposite of what you asked. But you can do the same with a "OutRented" container class. The point is to use a container class the has some knowledge of the class it's containing.


In reply to Re: oop, variable that counts the number of objects created by perlfan
in thread oop, variable that counts the number of objects created by Anonymous Monk

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 meditating upon the Monastery: (4)
As of 2024-04-19 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found