For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > getting the value of a var









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author getting the value of a var
Octavian Rasnita

2005-08-27, 6:55 pm

Hi,

I have a module like the one below:

package Teddy::Languages::ro::Help;

our $content = "here is the content...";


I want to get the value of $content from a program when a part of the name
of this module is given at runtime.
The program is:

my $lang = 'ro';
my $mod = 'Help';

I have tried:

my $module = "Teddy::Languages::${lang}::${mod}";
eval "require $module";

Then I want to print that value, but I don't know how.

I have tried:

print Teddy::Languages::$lang::$mod::content;
print $module::content;

I am missing something and I cannot make it work. Please help me.

Thank you.


Teddy


Matija Papec

2005-08-28, 6:55 pm

Octavian Rasnita wrote:

> my $module = "Teddy::Languages::${lang}::${mod}";
> eval "require $module";
>
> Then I want to print that value, but I don't know how.
>
> I have tried:
>
> print Teddy::Languages::$lang::$mod::content;
> print $module::content;
>
> I am missing something and I cannot make it work. Please help me.


If everything fails you can access package variables via symbol table,

#symbol table
my $st = eval '\%$module::';
# scalar reference
$sref = *{ $st->{content} }{SCALAR};
print $$sref;

perhaps you could rearrange your problem so it wouldn't use such access
to globals?
Matija Papec

2005-08-28, 6:55 pm

> #symbol table
> my $st = eval '\%$module::';


correction,
my $st = eval '\%'. $module .'::';

> # scalar reference
> $sref = *{ $st->{content} }{SCALAR};
> print $$sref;

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com