Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Stuck on packages

by Yoda_Oz (Sexton)
on Jan 04, 2007 at 03:41 UTC ( [id://592857]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, im having a small problem. im completely stuck and i dont know how to get unstuck...
i have some code here:
#!/usr/bin/perl use strict; use warnings; package Vehicle; sub new { my $class = @_; my $self = { _engine => "I have an engine.", _wheel => "I have wheels.", _door => "I have doors." }; bless $self, $class; return $self; } package Private; sub new { my $class = @_; my $self = { _Vehicle => new Vehicle(), _private => "I am private" }; bless $self, $class; return $self; } package Family; sub new { my $class = @_; my $self = { _Private => new Private(), _passengers => "I carry 5 passengers", _goods => "I carry 50lbs" }; bless $self, $class; return $self; } package main; my $car = new Family(); foreach $car (sort keys my %hash) { print "$car => $hash{$car}\n"; }
im trying to learn inheritance packages here and i cant seem to get a hold of it. what im trying to do is print out something from one package and all it's inheritances too...
does anyone get what i mean?
my output is nothing. no errors, nothing. just blank.
i was hoping to get printed, this:
I have an engine I have doors I have wheels I am private I carry 5 passengers I carry 50lbs

Replies are listed 'Best First'.
Re: Stuck on packages
by Trimbach (Curate) on Jan 04, 2007 at 04:29 UTC
    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

      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
        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.
Re: Stuck on packages
by GrandFather (Saint) on Jan 04, 2007 at 03:45 UTC

    What were you hoping would be printed? Data::Dump::Streamer can be used to show the contents of $car using Dump $car; in place of the print, (see below), but that's probably not what you are after.

    $Family1 = bless( { goods => 'I carry 50lbs', passengers => 'I carry 5 passengers', Private => bless( { private => 'I am private', Vehicle => bless( { doors => 'I have doors', engine => 'I have an engine' +, wheels => 'I have wheels' }, 'Vehicle' ) }, 'Private' ) }, 'Family' );

    DWIM is Perl's answer to Gödel
      i was hoping to get printed, this:
      I have an engine I have doors I have wheels I am private I carry 5 passengers I carry 50lbs
Re: Stuck on packages
by mreece (Friar) on Jan 04, 2007 at 06:38 UTC
    package Vehicle; sub new { my $class = @_; ...
    try my $class = shift; or my ($class) = @_;, but the above is assigning the 'length' of @_ (probably 1) to $class, so your objects aren't being properly blessed.

    (there are other problems with the code, see other responses, don't expect this comment to solve everything)

Re: Stuck on packages
by Tanktalus (Canon) on Jan 04, 2007 at 15:34 UTC

    One thing I want to point out. Generally, inheritance is used for "ISA" relationships (which is why Perl uses the "@ISA" array to declare inheritance). Where type A "IS A" type B. In your case, you sound like you want to say Family IS A Private which IS A Car. But that doesn't make sense: families are not cars! Families have cars, but are not cars themselves.

    Perhaps you meant that it was a FamilyCar. Or a Car::Family. Those would both say you're dealing with a specific type of car - the family car. (More commonly known as a sedan, I think). Names of your packages are important. They form part of your code-as-documentation. If you had the above code working the way you wanted, and then 12 months from now you came back as an OOP guru to fix something, you'd probably be confused. While that is normal when going from beginner to advanced, I'm hoping you can learn how important names are at the same time as you're learning the implementation details of OOP in Perl. This concept, however, is not Perl-specific. It applies no matter what language you're doing OOP in. Or procedural programming. Or functional programming. Names are always important.

      We have had sedans, station wagons and vans as family "cars". Perhaps what he needs is either multiple inheritance or a property to describe how the vehicle is used?

      Our current family sedan has a two tone colour scheme so rather than a colour property as a scalar, maybe a ColourScheme object as a property or mixin would be appropriate?

      DWIM is Perl's answer to Gödel

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 02:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found