Sherm Pendley <spamtrap at (no spam) shermpendley.com> writes:
"Sakthi" <ksakthiv at (no spam) gmail.com> writes:
I am getting a string value from a function .A variable is exist by that
string value. Need to print the value using the second variable!
Example:
my $log_dir="hello";
my $name=function1(); // $name has value "log_dir"
I want the output as "hello" using the "name" variable. Is that
possible?
No, because $log_dir is a lexical variable.
Yes, because eval works fine with lexical variables:
#!/usr/bin/perl
use strict;
use warnings;
my $log_dir="hello";
my $name = 'log_dir';
print eval "\$$name", "\n";
# prints "hello"
It works, but it's not a good idea.
Even if that were not the
case,
it would still be a terribly bad idea. Much better to use a hash:
my %dirs = (
'log_dir' => 'hello',
);
my $name = function1();
print $dirs{$name}, "\n";
See 'perldoc -q "variable as a variable name"' for details.
Agreed.
--
Keith Thompson (The_Other_Keith) kst-u at (no spam) mib.org
http://www.ghoti.net/~kst
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"