$ perl -Mdiagnostics -Mwarnings x Scalar value @persons[$counter] better written as $persons[$counter] at x line 263 (#1) (W syntax) You've used an array slice (indicated by @) to select a single el ement of an array. Generally it's better to ask for a scalar value (indicated by $). The difference is that $foo[&bar] always behaves like a scalar, both when assigning to it and when evaluating its argument, while @foo[&bar] behaves like a list when you assign to it, and provides a list context to its subscript, which can do weird things if you're expecting only one subscript. On the other hand, if you were actually hoping to treat the array element as a list, you need to look into how references work, because Perl will not magically convert between scalars and lists for you. See perlref. Content-type: text/html Pragma: no-cache Cache-control: no-cache Expires: Mon, 28 Apr 1997 00:01:00 -0500
Julien:
:CLASS: Person=HASH(0x1b9efbc)
               age => 22
             count => 0
              name => Julien
Leon:
:CLASS: Person=HASH(0x1b9510c)
               age => 19
             count => 0
              name => Leon
Odd number of elements in hash assignment at x line 162 (#2)

    (W misc) You specified an odd number of elements to initialize a hash, which

    is odd, because hashes come in key/value pairs.

Use of uninitialized value in array slice at x line 248 (#3)

    (W uninitialized) An undefined value was used as if it were already defined.
  It was
    interpreted as a "" or a 0, but maybe it was a mistake.  To suppress this
    warning assign a defined value to your variables.

Use of uninitialized value in hash element at x line 248 (#3)
Use of uninitialized value in addition (+) at x line 249 (#3)
CLASS: Adres=HASH(0x1b9519c)
                           => Person=HASH(0x1b9510c)
                         0 => street
           HASH(0x1b95124) => personCount
               personCount => 2
                    street => Promenade 21
              Promenade 21 <= street
                         2 <= personCount
             person number => 0
Use of uninitialized value in hash element at x line 263 (#3)
CLASS: Person=HASH(0x1b9510c)
               age => 19
             count => 0
              name => Leon
             person number => 1
CLASS: Person=HASH(0x1b9510c)
               age => 19
             count => 0
              name => Leon
#### use warnings; use strict; # use diagnostics; # verbose but helpful for learning. #### package Adres; sub new { my $prototype = shift; my $class = ref $prototype || shift; my $self = { '@persons' => [], # [] = arrayref; {} = hashref; () = list # a list like this must be an arrayref # (reference to an array) because a list () cannot # be held in an hash directly. street => '', }; bless $self, $class; return $self; } sub get_person_count { my $self = shift; return (scalar @{ $self->{'@persons'} }); } sub add_person { my $self = shift; while (@_) # allow adding multiple people { die "Error! $_ is not a Person object." unless (ref $_ && $_->isa('Person')); # note the magic clause "ref $x && $x->isa('Class')" # this catches child classes as well # and is similar in effect to the Java # expression "instanceof Class" push @{ $self->{'@persons'} }, $_; } } #### ##################################################### ## Class Constructor ##################################################### sub new { my $prototype = shift; my $class = ref $prototype || shift; # This will handle both types of constructor. my $self = { count => 0, name => '', age => 0 }; # No need to use int() in Perl. # Also, quotes are not needed to the left of => # (so long as it's alphanumeric) # Note that the quotes are required above, so that # perl can distinguish between '@persons' (a string) and # @persons (a nonexistant variable) # No need to use temporary variables here in Perl. # In fact, very rarely need to use them at all. if (@_) # are there more parameters? { $self->name(shift); } if (@_) # yet more? { $self->age(shift); } } #### sub age { my $self = shift; if (@_) # more parameters? { $self->{age} = shift; } return $self->{age}; }