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.
In reply to Using package variables dynamically by redhotpenguin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |