perl_noob_101 has asked for the wisdom of the Perl Monks concerning the following question:

sub new_car { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = { color => "bay", legs => 4, owner => undef, @_, # Override previous attributes }; return bless $self, $class;


1. Is the variable "self" a hash?
2. I need a mix data types. Pair type data (like I have currently) and a few arrays as well (like store the IDs under each color}. Is it possible to add array type data to a class?

If you guys can point me to a good resource to read, that would also be helpful.
Thanks in advance

Replies are listed 'Best First'.
Re: arrays in classes
by chromatic (Archbishop) on Jun 08, 2011 at 21:19 UTC
    Is the variable "self" a hash?

    $self contains a reference to a hash.

    Is it possible to add array type data to a class?

    Certainly. Store a reference to an array in your hash reference, for example.

    my $self = { color => "bay", legs => 4, owner => undef, aliases => [qw( Stumpy Blue Haze )], @_, };
    If you guys can point me to a good resource to read, that would also be helpful.

    I'm proud of the object chapters in my book Modern Perl. You can download it in several freely redistributable formats from that page.

Re: arrays in classes
by wind (Priest) on Jun 08, 2011 at 21:16 UTC
Re: arrays in classes
by locked_user sundialsvc4 (Abbot) on Jun 08, 2011 at 21:47 UTC

    The Perl way of doing things like this would be to define a car.pm module with a package car statement, which you would then incorporate into your program with a use car statement followed by my $car = car->new().   This subroutine, new(), would instantiate a hash, bless it so that it is now recognized to be “a car,” and return it to you.

    And this is how we do “objects” in a language that really didn’t know anything about them ... that is, until someone invented Moose.

    And now, please dis-regard everything that I just said, hold onto your hat, and click on that link at the end of the preceding paragraph ...

Re: arrays in classes
by perl_noob_101 (Initiate) on Jun 08, 2011 at 22:02 UTC

    Also is there a concept like dynamic array length in perl?? Thanks for the response

      Arrays are dynamically resized as needed in Perl. Perl doesn't have a concept of a statically sized array (or string for that matter).

      True laziness is hard work