perl: eval and constants


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: eval and constants
# 1  
Old 01-19-2005
Question 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 assign 2 to 1, 1 being the constant name. do i have to cave in and hardcode array 1?

thanks.
# 2  
Old 01-20-2005
Do you mean something like this?

Code:
#!/usr/bin/perl

@foo=('FOOD','NUMBER','OS');
@bar=('pizza','two','unix');

foreach $foo_val ( @foo ) {
  eval "\$$foo_val = @bar[$count]";
  ++$count;
}
print "\$FOOD IS $FOOD\n";
print "\$NUMBER IS $NUMBER\n";
print "\$OS IS $OS\n";

Cheers
ZB
# 3  
Old 01-20-2005
hey bob,

not quite; although i may end up using that method. i was trying to eval the variable names and values into a use constant $VAR_NAME => $VAR_VAL; type deal. after my efforts yesterday, i'm guessing it cannot be done, but if anyone has any further ideas, they are appreciated Smilie
# 4  
Old 01-20-2005
You can create constants dynamically using the "use constant", but the whole thing (initialization of constant-value hash) needs to be in a BEGIN {} block, before the rest of the script is executed.

This is good for initializing constants from config file or something like that before the script starts, but after the script has started execution there is no way to create real constants anymore.
# 5  
Old 01-20-2005
swell. Smilie this sets me on the proper path. thanks guys.
# 6  
Old 01-23-2005
#!/usr/bin/perl

@foo=('FOOD','NUMBER','OS');
@bar=('pizza','two','unix');

foreach $foo_val ( @foo ) {
eval "\$$foo_val = @bar[$count]";
++$count;
}
print "\$FOOD IS $FOOD\n";
print "\$NUMBER IS $NUMBER\n";
print "\$OS IS $OS\n";

exitSmilie))
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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... (1 Reply)
Discussion started by: vivek d r
1 Replies

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

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

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

6. UNIX for Dummies Questions & Answers

Operation with real variables and constants

Hello there, I'd like to define a variable b equal to 0.5/a where a=0.001, so I wrote something like that: a=0.001; let 'b=0.5/$a'; but it doesn't work... maybe because the variable a has a real value??? Any help will be appreciated!!!:D (1 Reply)
Discussion started by: Giordano Bruno
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. Programming

constants in C/C++

Hi all My question is related to following sample code which tries to change consant value by pointers.(I know it is wrong practice but i am surprised by mis-behaviour) The code: #include <stdio.h> int main() { const int x = 10; int *y; const int * const z = &x; y = (int *)&x;... (2 Replies)
Discussion started by: Shobhit
2 Replies
Login or Register to Ask a Question