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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Zhris
Enthusiast

Nov 2, 2013, 4:22 PM

Post #1 of 2 (1903 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


Code
#!/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)


Zhris
Enthusiast

Nov 4, 2013, 4:18 AM

Post #2 of 2 (1856 views)
Re: [Zhris] Access child global from parent package [In reply to] Can't Post

I've sorted this now.

Cheers,

Chris


(This post was edited by Zhris on Nov 4, 2013, 4:19 AM)

 
 


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

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