dereferenceing a string to a constant name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohanprasadgutta
    New Member
    • Dec 2007
    • 33

    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.
    [code=perl]
    my $name = "mohan";
    my $var = "name";
    my $new = $$name;
    print "your name is : " . $name . "\n";
    [/code]
    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.
    [code=perl]
    #file name : Test_Cfg.pm
    #!/usr/bin/perl
    package Test_Cfg;
    use constant NAME => "mohan";
    use constant USERID => "mpgutta";
    use constant EMPID => 12345;
    1;
    [/code]

    perl script is as follows.
    [code=perl]
    #!/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.
    [/code]

    could anybody assist me in getting the constant value from a string.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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?

    Comment

    Working...