Replacement for eval in Perl??????


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacement for eval in Perl??????
# 1  
Old 07-03-2008
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 urgently.

Thanks
Kunal
# 2  
Old 07-03-2008
There is an eval statement in perl as well.
# 3  
Old 07-03-2008
Quote:
Originally Posted by Annihilannic
There is an eval statement in perl as well.
Please, do not advocate the use of eval unnecessarily because it is a security risk. It should be reserved with tightly controlled commands and in this case this is certainly not tight controlled.

Perl does allow you reference a "variable variable" (a variable whose name is the value held by a variable). That works unless you are in strict mode, in which case an exception will be thrown.

Code:
$num = 60;
$varname = 'num';
print $$varname;    # 60

Even though that works, think about it carefully whether you really need that because not many use cases actually need this.
# 4  
Old 07-03-2008
Thanks for the reply.

I actually could not explain you my exact problem.

I want to do something like the below mentioned code...

VAR="USER_NAME"
VAR_VAL="User1"
eval $VAR=$VAR_VAL
echo "User Name is:$USER_NAME"


I am actually reading both values, for "VAR" and "VAR_VAL" ,from a file.
can i do this in perl?

Regards,
Kunal
# 5  
Old 07-03-2008
Why not? In that case you do not even need to use variable variables at all, if all of the data are from a file. Of course, how to do it depends on how you read from the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Word replacement in Perl

I have the following string : Cat dog fox catepillar bear foxy I need to replace "cat" and "fox" words from this sentence to word "animal" I do the following: $Str1="cat"; $Str2="fox"; $NewStr="animal"; open(F1, "<$inputFile") or die "Error: $!"; open(F2, ">$outputFile") or... (1 Reply)
Discussion started by: AIX_30
1 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. Shell Programming and Scripting

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 $t=10; $f='$t'; $result=eval "\$$f"; print "$result\n"; (3 Replies)
Discussion started by: prasperl
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 of sentence in perl

Hi, I have 3 arrays: @arr1=("Furthermore, apigenin treatment increased the level of association of the RNA binding protein HuR with endogenous p53 mRNA","one of the mechanisms by which apigenin induces p53 protein expression is enhancement of translation through the RNA binding protein... (1 Reply)
Discussion started by: vanitham
1 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

Replacement of sed with perl

Hi using the below cmd i am identifying wheether last character in each line in thousands of files as semicolon or not.If last character is semicolon i am removing semicolon .If last character is not semicolon then i am appending next line to present line . For example my input file consists of... (4 Replies)
Discussion started by: dbsurf
4 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