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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.