in reply to Invoking method in another package

Your usage is... unusual and not good design if you're just trying to do the typical object inheritance thing. Here I've altered your code to have meaningful package names, Cow inherits from Animal and the object variables make more sense for something that might be polymorphic later (cow became sound). If you had other animals you would just put the right data into the right variables and then rooster's could cluck. I added a roster class since it was trivial once the variables were changed around. Now if you *weren't* looking for polymorphic inheritance then speak up since that's what it looks like you're getting at.

my $fb = new Cow; $fb->sound; package Rooster; use base 'Animal'; sub new { bless { animal_type => 'rooster', sound => 'cluck' }, shift } package Cow; use base 'Animal'; sub new { bless { animal_type => 'cow', sound => 'moo' }, shift } package Animal; sub sound { my $self = shift; print $self->{animal_type} . "s go: " . $self->{sound}; }

Replies are listed 'Best First'.
Re: Re: Invoking method in another package
by nutshell (Beadle) on Sep 30, 2002 at 01:10 UTC
    What I'm trying to do is just pass the blessed hash reference along to Barfoo::thisone so I can rip values from it. Maybe I'm just better off using global variables :|

      Oh ok, I can understand that. I've been writing code in a similar vein except mine steals lexical variables. If you want to steal variables out of a blessed something then what you originally wrote is perfectly fine - you might want to see about importing that thisone() method or name it something that indicates it's integrity violating nature. So what your call to Foobar::thisone($obj) is semantically equivalent to thisone($obj) except that one version leaves the method off in the original package. If you look at my signature you'll see an example of a something being violated in the exact same way. I leave the B::svref_2object method over there since I didn't feel like importing it into the current scope. At that point it becomes preference and style.