Perl preprocessor appended to other variable

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;

Answers


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.


Need Your Help

Python write string "1001101" as binary stream to file

python string file binary output

I have a very long string that is 1's and 0's. It's about 12 32-bit assembly instructions. I want to write this stream to a .bin file... but I don't want it to be in ASCII format. I want just th...

Unable to start debugging in Visual Studio RTM for cordova app

visual-studio-2015 visual-studio-cordova

I have a project that was working on RC and worked with no problems.