468,321 Members | 1,733 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

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

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
11 1061
KevinADC
4,059 Expert 2GB
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
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
3,503 Expert Mod 2GB
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
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
Thanks Kevin,

Let me try this
Jul 28 '08 #8
Kevin,
No it is not working it is giving me the output as follow.

DB_PATH....$VAR1 = undef;

-Chiku
Jul 28 '08 #9
KevinADC
4,059 Expert 2GB
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
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
3,503 Expert Mod 2GB
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.

Similar topics

9 posts views Thread by Krisztian Kepes | last post: by
38 posts views Thread by Red Dragon | last post: by
1 post views Thread by Rahul | last post: by
1 post views Thread by treelife | last post: by
83 posts views Thread by deppy_3 | last post: by
2 posts views Thread by Chris Smith | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by howard w | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.