Perl eval problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl eval problem
# 1  
Old 03-08-2013
Perl eval problem

Hello All,

I am trying to use perl eval in a complex code and below given is a pseudo code of my logic.

Here $result evalutes to empty.


Please help.How should I retrieve of $t where $f just hold the name of varaible i.e t

Code:
$t=10;
$f='$t';
$result=eval "\$$f";
print "$result\n";

# 2  
Old 03-08-2013
Code:
$t=\10;
$f='$t';
$result=eval "\$$f";
print "$result\n";

OR
Code:
$t=10;
$f='t';
$result=eval "\$$f";
print "$result\n";

You would also need to worry about the eval string's scope.

Last edited by elixir_sinari; 03-08-2013 at 02:40 PM..
# 3  
Old 03-08-2013
The usual thing to do in Perl is, instead of looking up variable names, keep a hash to look up values in.

Code:
my %hash=( "key" => "value",
                               "another key" => "another value" );

print "value for var 'key' is ",$hash{key},"\n";

This is also much more secure than eval. Someone could put `rm -Rf ~/` in your eval and it would do it!

Last edited by Corona688; 03-10-2013 at 03:26 PM..
# 4  
Old 03-11-2013
Code:
perl -e '$t=10;
$f=q($t);
$result=eval $f;
print "$result\n";'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with eval a generated and interpreted variable name and its value

Heya I have modified $HOME/.config/user-dirs.dirs to my like and the variables are properly 'structured' (XDG_XY_DIR="$HOME/YZ"). Now since i wrote a script to do that part, i currently stuck at the part to create a new .gtk-bookmarks file. Regarding code snippet: (bold part beeing... (2 Replies)
Discussion started by: sea
2 Replies

2. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (13 Replies)
Discussion started by: vivek d r
13 Replies

3. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Web Development

problem with exporting vairable from one perl cgi to another perl cgi script while redirecting.

Can anyone tell me how to export a variable from one perl CGI script to another perl cgi script when using a redirect. Upon running the login.pl the user is prompted to enter user name and password. Upon entering the correct credentials (admin/admin) the user is redirected to welcome page. My... (3 Replies)
Discussion started by: Arun_Linux
3 Replies

5. Shell Programming and Scripting

Help with perl eval command .....

Hi All, I read the above written code (perl code) in another perl script and evaluates this code for each line of text file,but using exit statement in code make this not to work and i could not get the desired results. However if i use return it works fine. I just need to know why it doesn't... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

7. Shell Programming and Scripting

Replacement for eval in Perl??????

I used the eval command in shell programming for assigning a value to a stored value of a variable. Example: VAR="Unix_Id" eval $VAR="101" eval echo $"$VAR" How can i assign a value to a stored value of a variable in perl OR how i can write above code in Perl? I need your help... (4 Replies)
Discussion started by: kunal_dixit
4 Replies

8. Shell Programming and Scripting

redefine warning(Eval) in perl

Here is my perl Program: #!/usr/bin/perl -w my $a="sam"; my $b="ste"; my $c="abcdef"; my $d=931; $str=" @<<<<< @>>>>>>>>>> @|||||||||||||||||||| @######### \$a,\$b,\$c,\$d ."; open(FILE,">abc.txt"); $temp="format FILE = $str"; eval $temp; write FILE; print FILE "\n\n"; (3 Replies)
Discussion started by: sameerstephen
3 Replies

9. Shell Programming and Scripting

eval problem.. advice needed!

Hi I need some major help with eval I have a statement using eval: read input eval variable$input=”something” Now I want to use the “variable$input” in some commands but I don't know how to call it without replacing the $input with the command line value (which I obviously can't do). ... (1 Reply)
Discussion started by: Cactus Jack
1 Replies

10. Shell Programming and Scripting

perl: eval and constants

is it possible to use eval to create constants in perl? i cannot seem to get anything to work, and my searches are turning up little to nothing. an example of what i am trying to do is this: 2 arrays: array 1: 'FOOD','NUMBER','OS' array 2: 'pizza','two','unix' loop through the arrays and... (5 Replies)
Discussion started by: effigy
5 Replies
Login or Register to Ask a Question