By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Help to set value for some constant.. Using constant module

P: 14
Can anybody help me to solve the following problem?
I am using constant module. And trying to set the value for some constant.

This is my work code so I am using the trial code.

Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use strict;
  3. use constant;
  4. use Data::Dumper;
  5.  
  6. my @admin_dir;
  7. @admin_dir = ("/home/prd/test.txt", "DB_USER");
  8. my $var = $admin_dir[0];
use constant DB_PATH => "/home/prd/test.txt"; ## instead of hard coding the path I want to retrieve the path from the array @admin_dir

I used the following .....
Expand|Select|Wrap|Line Numbers
  1. use constant DB_PATH => \$var;
  2. print "DB_PATH ...". Dumper(DB_PATH)."\n";
The output is
Expand|Select|Wrap|Line Numbers
  1. DB_PATH ....$VAR1 = \'/home/prd/test.txt';
and I want output as
Expand|Select|Wrap|Line Numbers
  1. DB_PATH ....$VAR1 = '/home/prd/test.txt';
Jul 28 '08 #1
Share this Question
Share on Google+
11 Replies


KevinADC
Expert 2.5K+
P: 4,059
instead of this:

Expand|Select|Wrap|Line Numbers
  1. use constant DB_PATH => \$var;
do this:

Expand|Select|Wrap|Line Numbers
  1. use constant DB_PATH => $var;
Jul 28 '08 #2

P: 14
Kevin,

I already tried that but ... output is
DB_PATH ....$VAR1 = undef;

I tried all the possible tricks... and was unsuccessful
Jul 28 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
Kevin,

I already tried that but ... output is
DB_PATH ....$VAR1 = undef;

I tried all the possible tricks... and was unsuccessful
Have you tried placing the $var in double quotes? I would say try everything you can till it works, but try that and lets see.

Regards,

Jeff
Jul 28 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
OK, I had to read the "constant" pragmas documentation. It defines the constants at compile time so you can't do what you are tring to do: define a constant at run time. You maybe can do it if you wrap it all in a BEGIN{} block. Untested code:


Expand|Select|Wrap|Line Numbers
  1. BEGIN {
  2.    my @admin_dir = ("/home/prd/test.txt", "DB_USER");
  3.    my $var = $admin_dir[0];
  4.    use constant DB_PATH => $var;
  5. }
  6. use warnings;
  7. use strict;
  8. use Data::Dumper;
  9.  
Jul 28 '08 #5

P: 14
Jeff,
I tried placing $var in double quotes, single quotes( I know it will take it as literal), scalar $var, eval($var).

Actually it is working for my rest of the code

for example
Expand|Select|Wrap|Line Numbers
  1. use constant DB_USERS => {
  2.         test => 'user1'
  3.         dev  =>  'user2'
  4.         prd  =>  'user3'
  5.         }
  6.  
here test, dev, prd are the environment and their users.
Expand|Select|Wrap|Line Numbers
  1. my array1 = (test, user1)
  2.  
when I am using the following

use constant DB_USERS = {}; ## anonymous hash
DB_USERS->{@array1[0]} = @array1[1] ## placing value in the hash.
this is working and gives me required output. But it is not working when I have scalar variable.
Jul 28 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
See my post above, and try this code:

Expand|Select|Wrap|Line Numbers
  1. BEGIN {
  2.    use strict;
  3.    use warnings;
  4.    my @admin_dir = ("/home/prd/test.txt", "DB_USER");
  5.    my $var = $admin_dir[0];
  6.    use constant DB_PATH => $var;
  7. }
  8.  
  9. use Data::Dumper;
  10.  
I am not sure it will work and not sure what will happen if you change the value of $var later in the program.
Jul 28 '08 #7

P: 14
Thanks Kevin,

Let me try this
Jul 28 '08 #8

P: 14
Kevin,
No it is not working it is giving me the output as follow.

DB_PATH....$VAR1 = undef;

-Chiku
Jul 28 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
This seems to work but I really am not sure how wise this is:

Expand|Select|Wrap|Line Numbers
  1. BEGIN {
  2.    our @admin_dir = ("/home/prd/test.txt", "DB_USER");
  3.    our $var = $admin_dir[0];
  4. }
  5. use strict;
  6. use warnings;
  7. use constant DB_PATH => $var;
  8. use Data::Dumper;
  9. print Dumper DB_PATH;
Jul 28 '08 #10

P: 14
Thank you Kevin,
It is working

Kevin and Jeff
THANKS FOR ALL YOUR EFFORTS. I really appreciate your help.

-Chiku
Jul 28 '08 #11

numberwhun
Expert Mod 2.5K+
P: 3,503
Chiku,

I am glad its working for you. You can thank Kevin for the solution.

On a side note, you need to please use code tags around ANY code that you put in forums. If you aren't sure how to use them, then edit your original post and see the tags that were placed around your code by eWish.

Regards,

Jeff
Jul 29 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.