in reply to oop, variable that counts the number of objects created
Do:my $auto = new car("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.my $auto = car->new("Opel Vectra",50000);
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.
PS: nice you're not using an OOP framework, because you don't need one xDuse 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;
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.
|
|---|