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
- my $name = "mohan";
- my $var = "name";
- my $new = $$name;
- print "your name is : " . $name . "\n";
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
- #file name : Test_Cfg.pm
- #!/usr/bin/perl
- package Test_Cfg;
- use constant NAME => "mohan";
- use constant USERID => "mpgutta";
- use constant EMPID => 12345;
- 1;
Expand|Select|Wrap|Line Numbers
- #!/usr/bin/perl
- my $mode = $ARGV[0];
- print $mode; # This is just printing the arguement. not the costant value.
- print ${"$mode"}; # this is not printing anything at all.