in reply to How not to use "our"?
Here's an example of how to "access a common variable but without resorting to globals":
use strict; use warnings; package A; { my $maxlengths = { tinytext => 100, longtext => 2000, }; sub max_tiny_text { return $maxlengths->{tinytext}; } sub max_long_text { return $maxlengths->{longtext}; } } package B; use base 'A'; print 'From B: ', A->max_tiny_text(), "\n"; print 'From B: ', A->max_long_text(), "\n"; package main; print 'From main: ', B->max_tiny_text(), "\n"; print 'From main: ', B->max_long_text(), "\n";
The output from this is:
From B: 100 From B: 2000 From main: 100 From main: 2000
Here's a quick rundown of what I've done here and why.
I've aimed to keep the same general framework you presented. There are (as usual) more ways to do it. :-)
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How not to use "our"?
by JavaFan (Canon) on Nov 29, 2010 at 21:32 UTC | |
|
Re^2: How not to use "our"?
by Anonymous Monk on Nov 29, 2010 at 16:42 UTC | |
by kcott (Archbishop) on Nov 29, 2010 at 17:18 UTC |