redhotpenguin has asked for the wisdom of the Perl Monks concerning the following question:
I'm attempting to access package variables dynamically from other packages. My explanation and terminology may not very clear, so here are some code snippets which show what I am attempting to do.
A package which represents a model subclass.
The base model packagepackage my::model::subclass; use strict; use warnings; use base qw/my::model/; our %normal = ( top_level_key => { name1 => value1, name2 => value2, }, ); # the rest of the package...
package my::model; use strict; use warnings; sub use_subclass_package_var { my ($self, @cols) = @_; my $normal = join '::', ref $self, 'normal'; foreach my $col (@cols) { # # I need help getting the next line to work properly # next unless exists eval { %{$normal{$col}} }; # the rest of the code } }
Example usage:
my $obj = my::model::subclass->new(); $obj->use_subclass_package_var('top_level_key');
So what I am trying to do here in use_subclass_package_var is check for the key $col in the hash %my::model::subclass::normal, i.e. this would be true if ($col eq 'top_level_key').
I came across How to reference variables in another package via keyword names which appears to similiar to my situation, but my monk mojo is not strong enough yet understand it fully.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using package variables dynamically
by jdporter (Paladin) on Feb 08, 2005 at 18:39 UTC | |
by redhotpenguin (Deacon) on Feb 08, 2005 at 18:53 UTC | |
|
Re: Using package variables dynamically
by nobull (Friar) on Feb 08, 2005 at 18:32 UTC | |
|
Re: Using package variables dynamically
by chromatic (Archbishop) on Feb 08, 2005 at 18:35 UTC | |
|
Re: Using package variables dynamically
by revdiablo (Prior) on Feb 08, 2005 at 18:41 UTC |