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.
-
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.