Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Inheritance and package variables

by submersible_toaster (Chaplain)
on Jul 26, 2004 at 05:59 UTC ( [id://377360]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks,
I hope somebody can steer me in the correct direction with this. I am trying to write a group of packages that inherit two methods (maybe more) from a base class. I arrived at this idea after noticing myself copying and pasting code into sub packages. These inherited methods are the constructor and an initialization method.

What is tripping me over is that I figured on having a description of what attributes and what type of child=>package relationship each package accepts, as part of the package definition. Sadly , I think it is my poor understanding of scope that is the true problem.

Below is some code that demonstrates how my misunderstanding unravels. Turning off strict refs in order to symboliclly address a $Some::Package::variable seems evil and I don't like it.

UPDATE:I think what I am really asking is, how might a method defined in package Guff , address a variable belonging to the package it's object is blessed into, that inherits Guff. Clear as mud? I think I am confusing myself more.

Test script

#!/usr/bin/perl -w use strict; use lib './'; use Guff; use Data::Dumper; my $data = { type=>'first', value=>0, DESKTOP=> { type=>'second', value=>1, LIBRARY=> { value=>2, type=>'third', REEL=>{ type=>undef }, } }, }; my $g = Guff->new( $data ); print Dumper $g ;

The packages

package Guff; use Carp; use strict; use Guff::Child1; use Data::Dumper; our $child = 'DESKTOP'; our %dispatch = ( first=>'Guff::Child1', ); sub new { my $class = shift; my $in = shift; #my %opt = %{$_[0]} ; my %opt = %{$in}; my $kid = $class . "::child"; no strict 'refs'; do { confess "Missing child attribute $kid ", $$kid unless exists $opt{$$kid}; } if defined ( $opt{$$kid} ); use strict 'refs'; my $self = bless \%opt, $class; $self->init; return $self; } sub init { my $self = shift; my $child = $self->childkey; return unless defined $self->{type}; confess "Invalid child $child -" , Dumper $self unless exists $self->{ $child }; if ( $self->dispatchto ) { $self->{ $child } = $self->dispatchto->new ( $self->{ $child } ) or confess "Failed to dispatch " , Dumper $self; } } sub childkey { my $self = shift; my $kid = ref($self) . "::child"; no strict 'refs'; my $child = $$kid; use strict 'refs'; return $child; } sub dispatchto { my $self = shift; my $key = shift; my $dsp = ref($self) . "::dispatch"; no strict 'refs'; my $dispatch = $$dsp{$self->{type}}; use strict 'refs'; return $dispatch; } 1;
package Guff::Child1; use Guff; @ISA = qw/Guff/; our %dispatch = ( second=>'Guff::Child2', ); our $child = 'LIBRARY'; 1; package Guff::Child2; use Guff::Child3; @ISA = qw/Guff::Child1/; our %dispatch = ( third=>'Guff::Child3', ); our $child = 'REEL'; 1; package Guff::Child3; use Guff::Child2; @ISA = qw/Guff::Child2/; our %dispatch = ( ); our $child = undef; 1;

I can't believe it's not psellchecked

Replies are listed 'Best First'.
Re: Inheritance and package variables
by broquaint (Abbot) on Jul 26, 2004 at 06:15 UTC
    Since perl's implementation OO doesn't cover variables this the place to use accessor methods e.g
    use base 'Class::Accessor'; Guff->mk_accessors(qw/ child dispatch /); ## later on return $self->dispatch;
    That's just one approach, but it's basically illustating how you might implement accessors to your package variables. See. Class::Accessor for more information on its usage.
    HTH

    _________
    broquaint

Re: Inheritance and package variables
by dsb (Chaplain) on Jul 26, 2004 at 11:46 UTC
    Using the fully qualified name of the package variable should accomplish what you want to do, if I am understanding you correctly.

    Fully qualify a variable by adding the package it belongs to to any instances of its use.

    $package::varname $DBI::errstr $main::var
    Note that any variables declared with my don't belong to any package and so could not be accessed like this.



    dsb
    This is my cool %SIG
Re: Inheritance and package variables
by Your Mother (Archbishop) on Jul 27, 2004 at 05:46 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://377360]
Approved by DigitalKitty
Front-paged by Thelonius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-19 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found