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

dereferenceing a string to a constant name

P: 33
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. file is as follows.
Expand|Select|Wrap|Line Numbers
  1. #file name :
  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
Share this Question
Share on Google+
1 Reply

Expert 2.5K+
P: 4,059
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.