Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Stuck on packages

by Trimbach (Curate)
on Jan 04, 2007 at 04:29 UTC ( #592865=note: print w/replies, xml ) Need Help??

in reply to Stuck on packages

There's a number of things wrong here. First off Perl inheritence doesn't work like you've coded... you're injecting objects into class variables, which can certainly be useful, but it isn't inheritence and doesn't let you aggregate methods like you want. Inheritence in Perl revolves around the @ISA array. The @ISA array tells each package (class) that it "is a" member of another class, and tells Perl where to inherit methods from.

Secondly, if you want to display the attributes of a class instance you need to call the methods of that instance. Your three example classes don't have any methods, and print($car) just prints a stringified version of the $car object, which is the type of variable $car represents followed by its location in memory. To get the behavior you want you should do something like $car->print where "print" represents a method in the Family package that calls "getter" methods for each attribute you want to display.

I would strongly recommend Damian Conway's Object Oriented Perl if you want to learn (most) all there is to know about Perl 5 OO programming.

Gary Blackburn
Trained Killer

Replies are listed 'Best First'.
Re^2: Stuck on packages
by Yoda_Oz (Sexton) on Jan 04, 2007 at 04:46 UTC
    how would i write a "getter" method?
      A simple getter method might look like:
      sub get_foo { my $self = shift; return $self->{foo}; }
      use strict; use warnings; package Foo; sub new { my $class = shift; return bless { foo => "bar!\n" }, $class; } sub get_foo { my $self = shift; return $self->{foo}; } 1; my $foo = Foo->new; print $foo->get_foo;
      The above code will print bar! There are many modules on CPAN that can create getter and setter methods for you, Class::Accessor and Class::MethodMaker are a couple of examples
        ok i understand now!
        so what would i need to code if i wanted something like this?
        sub new { my $class = shift; return bless { engine => "engine", wheels => "wheels", etc...}, $c +lass; } sub get_foo { my $self = shift; return $self->{engine,wheels,etc...}; }
        how do i make it an array or hash or whatever?
      You've already had Damian's book recommended to you. For instant gratification, look at perlboot. You seem to be lacking some basic understanding of how OOP works in Perl.
      A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://592865]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2023-06-08 16:01 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (34 votes). Check out past polls.