CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate: Access child global from parent package: Edit Log


Nov 2, 2013, 4:22 PM

Views: 4339
Access child global from parent package


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,


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)

Edit Log:
Post edited by Zhris (Enthusiast) on Nov 2, 2013, 4:23 PM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives