
Zhris
Enthusiast
Nov 2, 2013, 4:22 PM
Post #1 of 2
(4406 views)
|
Access child global from parent package
|
Can't Post
|
|
Hi, I have a set of child packages which share numerous methods from a parent package. Each child package has a global variable that I would like to access from the parent package upon instantiation. I have tried different ways of achieving the above, but have found the only way I can access is by using eval via eval '$' . $class . '::listref'. I would like to avoid using eval if possible. Surely there is an alternative? If possible, I would like to keep the global. Thanks in advance, Chris http://codepad.org/bNJMRusI
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; package myparent; { sub new { my $class = shift; my $self = { }; bless $self, $class; $self->{listref} = eval '$' . $class . '::listref'; return $self; } } package mychild; { use base 'myparent'; our $listref = [ 1, 2, 3 ]; } package main; { my $obj = mychild->new; print Dumper $obj; }
(This post was edited by Zhris on Nov 2, 2013, 4:23 PM)
|