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
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.
$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.
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
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.