0

I am trying to write following code, where I want $varPRE1 to be $var1 and $varPRE2 to be $var2 after preprocessor is evaluated, but it is not working. Is there a work around?

#define PRE1 1
#define PRE2 2

my $var1 = 10;
my $var2 = 20;

print $varPRE1;
print $varPRE2;
1
  • 3
    You almost certainly want to use some data structure other than plain scalars. What problem are you trying to solve?
    – darch
    Jul 9, 2015 at 2:10

1 Answer 1

2

What you wrote doesn't make sense even for the C preprocessor, since varPRE1 would get parsed as one token.

What you want to do is sort of possible, using what are called symbolic references, but it is a bad idea.

$PRE1 = '1';
$PRE2 = '2';
$var1 = 10;
$var2 = 20;
print ${'var' . $PRE1};   #   same as print $var1 => 10
print ${'var' . $PRE2};

This will not work under use strict refs. It is also a bad idea to not use strict refs.

1
  • I would suggest this answer would benefit from an example of using a hash.
    – Sobrique
    Jul 9, 2015 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.