codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  dereferenceing a string to a constant name  mohanprasadgutta at 12:21 on Thursday, September 18, 2008
 

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.

  Re: dereferenceing a string to a constant name  hermanningjaldsson at 22:18 on Thursday, September 18, 2008
 

#!/usr/bin/perl

use lib "somepath";
#change this to whatever path you have themodule.pm at.
use themodule;

my $mode = $ARGV[0];

$NAME = NAME;
$USERID = USERID;
$EMPID = EMPID;

print STDOUT "\n\n\n$$mode\n\n\n";





############################################################
#themodule.pm has the following content:
package themodule;
require Exporter;

our $VERSION = 1.00;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw($weight);
our @EXPORT = qw
(
NAME
USERID
EMPID
);

use constant NAME => "mohan";
use constant USERID => "mpgutta";
use constant EMPID => 12345;

1;


#let me know how it goes.

  Re: dereferenceing a string to a constant name  hermanningjaldsson at 09:03 on Friday, September 19, 2008
 

#!/usr/bin/perl
use lib "SOMEPATH";#remember to change this.
use themodule;

my $cconstant = $ARGV[0];

my %constants_raw = THECONSTANTS;
my %constants = fix_hash_after_filling_from_constants(%constants_raw);

print STDOUT "\n\n\n$constants{$cconstant}\n\n\n";


sub fix_hash_after_filling_from_constants{
######################################################
#com.
my %temp = @_;

foreach my $ckey (keys %temp)
{
my $thevalue = $temp{$ckey};
delete $temp{$ckey};

$ckey =~ s/,//gi;
$thevalue =~ s/,//gi;
$thevalue =~ s/\"//gi;

$temp{$ckey} = $thevalue;
}

return %temp;
}



#the following is the module.
package themodule;
require Exporter;

our $VERSION = 1.00;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw($weight);
our @EXPORT = qw
(
THECONSTANTS
);

use constant THECONSTANTS => qw
{
NAME, "mohan",
USERID, "mpgutta",
EMPID, 12345,
};

1;



=pod
let me know.
=cut




  Re: dereferenceing a string to a constant name  hermanningjaldsson at 10:24 on Friday, September 19, 2008
 

beware that after the constants have been put into the hash that we are now
dealing with variables which are not constants anymore.

and since the constants were problably made constants for a reason,
this can be quite dangerous.



  Re: dereferenceing a string to a constant name  mohanprasadgutta at 10:34 on Friday, September 19, 2008
 

hi..
the code is working perfectly.
thank you very much for your help.
Best regards,
Mohan

  Re: dereferenceing a string to a constant name  hermanningjaldsson at 10:43 on Friday, September 19, 2008
 

#!/usr/bin/perl
use lib "somepath";#remember to change this
use themodule;

my $cconstant = $ARGV[0];
print STDOUT THECONSTANTS->{$cconstant};







#and now the module.
package themodule;
require Exporter;

our $VERSION = 1.00;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw($weight);
our @EXPORT = qw
(
THECONSTANTS
);

use constant THECONSTANTS =>
{
NAME => mohan,
USERID => mpgutta,
EMPID => 12345,
};

1;



#this solution does not produce the danger of changeable constants.
#and its simpler and smaller.



  Re: dereferenceing a string to a constant name  GH89 at 14:00 on Monday, January 17, 2011
 

Wonderful.

Share a website with you ,

put this url in google sirch

( aamall.us )

Believe you will love it.

We accept any form of payment








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Digital.Canal.Software.V2013
•  Altair_HyperWorks_12.0.1_Win64
•  Global.mapper.V15.0
•  ESI.PAM-RTM.V2010.0
•  KISSsoft.03.2013E.SP5
•  Technical.Toolboxes.Pipeline.Toolbox.2013.Enterprise.v15.0.0
•  Simprotek.Simprosys.V3.0
•  Ricardo.SABR.V6.0p1
•  PSIM.V9.2


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2013