reyjrar has asked for the wisdom of the Perl Monks concerning the following question:
As you probly already know this produces an error about $Name being undeclared. So I tried this:package Obj; use strict; our $Name; sub new { my $proto = shift; my $class = ref $proto || $proto; my $self = bless {}, $class; return $self; } sub test { my $self = shift; print "$Name \n"; } package Test; use strict; our @ISA=("Obj"); $Name = 'Testing'; package main; my $t = new Test; $t->test;
Which results in printing nothing. So then I remember TheDamian's book, and the "SUPER::" package, so I figure if I _HAVE_ to I'll do this:#$Name = "Testing"; TO our $Name = "Testing";
I get the same results as before. Nothing is printed. So I delve into perltoot, perltooc, perlbot, and perlobj which present similar but not exactly the same thing style examples. Confused and wondering if I've been dreaming for the past few years of perl development, I hardcore the package for a sanity check:#$Name = "Testing"; TO $SUPER::Name = "Testing";
This code actually works and produces the desired output. Leaving me completely Confused. Is "SUPER::" only for subroutine location? I want to abstract out the "Obj" class from the "Test" Class. Ideally, I'd like the the test() method in the the Obj class be able to use the current package's $Name. What is the best way to acheive this?#$Name = "Testing"; TO $Obj::Name = "Testing";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Package Variables in Inheritance
by broquaint (Abbot) on Oct 31, 2003 at 19:40 UTC | |
|
Re: Package Variables in Inheritance
by tilly (Archbishop) on Oct 31, 2003 at 20:25 UTC | |
|
Re: Package Variables in Inheritance
by perrin (Chancellor) on Oct 31, 2003 at 19:52 UTC | |
by reyjrar (Hermit) on Oct 31, 2003 at 20:15 UTC | |
|
Re: Package Variables in Inheritance
by hardburn (Abbot) on Oct 31, 2003 at 20:50 UTC | |
by reyjrar (Hermit) on Oct 31, 2003 at 21:11 UTC | |
by ysth (Canon) on Nov 02, 2003 at 08:59 UTC | |
by tilly (Archbishop) on Nov 02, 2003 at 17:12 UTC | |
|
Re: Package Variables in Inheritance
by freddo411 (Chaplain) on Nov 01, 2003 at 01:05 UTC |