P
Prabh
Hello all,
I've a question about the using the eval function at runtime.
I can evaluate a varible, $foo, as
my $bar = eval($foo) ;
But when theres some extra string at the end of $foo, the eval fails
to return any value,
my $bar = eval($foo/moretext) ;
The value of $foo is to be interpolated at the run-time.
If at runtime it interpolates as, "/home/123user", is it possible for
me to eval my $bar as "/home/123user/moretext".
Reason I ask,
I've a file full of property settings, one of which sets the location
of logfile, as follows,
logfile = ${USER_HOME}/logfiles/log.txt
My program traverses through this properties file, finds a match for
logfile setting, gets the value of this setting and needs to expand
the USER_HOME variable as follows,
==============================================================================
if ( $line =~ /^logfile/ )
{
my $log_to_file = (split(/(\s)*\=(\s)*/, $line))[1] ;
# BTW, is 'split' the best way to obtain the value.
# I tried using the regular expressions, but cant find a way to do
# w/o an 'if conditional.'
}
$log_to_file_after_eval = eval($log_to_file) ;
==============================================================================
The eval returns null. So, I'm doing it the hacky way, extracting the
environment variable part of it ( the text between "${" and "}" ) and
splitting it from the rest of the setting value.
==============================================================================
if ( $log_to_file =~ m/^\s*\$\{(.*)\}(.)*/ )
{
$user_home_path = $1 ;
$rest_of_path = $2 ;
# Put the "${ENV" in front of it, to supply it to eval.
$user_home_path = '$ENV{'."$user_home_path"."}' ;
}
$after_eval = eval($user_home_path} ;
$user_home_after_eval = "$after_eval"."$rest_of_path" ;
print "This is path after eval: $usr_home_after_eval\n";
==============================================================================
I'm wondering if theres a more elegant solution for this.
I've a feeling this might get more and more complicated, like some
users may not wrap the variable with {}.
Thanks for your time,
Prab
I've a question about the using the eval function at runtime.
I can evaluate a varible, $foo, as
my $bar = eval($foo) ;
But when theres some extra string at the end of $foo, the eval fails
to return any value,
my $bar = eval($foo/moretext) ;
The value of $foo is to be interpolated at the run-time.
If at runtime it interpolates as, "/home/123user", is it possible for
me to eval my $bar as "/home/123user/moretext".
Reason I ask,
I've a file full of property settings, one of which sets the location
of logfile, as follows,
logfile = ${USER_HOME}/logfiles/log.txt
My program traverses through this properties file, finds a match for
logfile setting, gets the value of this setting and needs to expand
the USER_HOME variable as follows,
==============================================================================
if ( $line =~ /^logfile/ )
{
my $log_to_file = (split(/(\s)*\=(\s)*/, $line))[1] ;
# BTW, is 'split' the best way to obtain the value.
# I tried using the regular expressions, but cant find a way to do
# w/o an 'if conditional.'
}
$log_to_file_after_eval = eval($log_to_file) ;
==============================================================================
The eval returns null. So, I'm doing it the hacky way, extracting the
environment variable part of it ( the text between "${" and "}" ) and
splitting it from the rest of the setting value.
==============================================================================
if ( $log_to_file =~ m/^\s*\$\{(.*)\}(.)*/ )
{
$user_home_path = $1 ;
$rest_of_path = $2 ;
# Put the "${ENV" in front of it, to supply it to eval.
$user_home_path = '$ENV{'."$user_home_path"."}' ;
}
$after_eval = eval($user_home_path} ;
$user_home_after_eval = "$after_eval"."$rest_of_path" ;
print "This is path after eval: $usr_home_after_eval\n";
==============================================================================
I'm wondering if theres a more elegant solution for this.
I've a feeling this might get more and more complicated, like some
users may not wrap the variable with {}.
Thanks for your time,
Prab