|
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.
|
|
|
#!/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.
|
|
|
#!/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
|
|
|
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.
|
|
|
hi..
the code is working perfectly.
thank you very much for your help.
Best regards,
Mohan
|
|
|
#!/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.
|
|
|
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
|
|
|