469,275 Members | 1,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

dereferenceing a string to a constant name

hello everyone,
i have to write a program which will take a command line arguement which is nothing but a constant name. in the program i am trying to print out the value corresponding to the constant name provided.
i am aware of converting a string to a variable name as follows.
Expand|Select|Wrap|Line Numbers
  1. my $name = "mohan";
  2. my $var = "name";
  3. my $new = $$name;
  4. print "your name is : " . $name . "\n";
But here i have to get the constant value corresponding to the command line arguement passed.
I have tried in the following way.
i have kept the constants in a pm file.
Test_Cfg.pm file is as follows.
Expand|Select|Wrap|Line Numbers
  1. #file name : Test_Cfg.pm
  2. #!/usr/bin/perl
  3. package Test_Cfg;
  4. use constant NAME => "mohan";
  5. use constant USERID => "mpgutta";
  6. use constant EMPID => 12345;
  7. 1;
perl script is as follows.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. my $mode = $ARGV[0];
  3. print $mode; # This is just printing the arguement. not the costant value.
  4. print ${"$mode"}; # this is not printing anything at all.
could anybody assist me in getting the constant value from a string.
Sep 18 '08 #1
1 2168
4,059 Expert 2GB
Your question with the sample code is confusing. You say your perl program will take a command line argument, but then you show that the "name" comes from an external file, which is it?
Sep 18 '08 #2

Post your reply

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

Similar topics

5 posts views Thread by lawrence | last post: by
3 posts views Thread by Paul Kirby | last post: by
10 posts views Thread by baje | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.