View Full Version : How do you get the name of variable?
super_gunda
08-01-2007, 05:48 PM
Hi,
I am checking if a variable is defined or not.
If the variable is not defined, i want the print the variable name and say 'this variable is not defined'.
Do any of you know any hack to print the name of the variable?
Thanks!
R
FishMonger
08-01-2007, 06:07 PM
Do you want this:
use strict;
use warnings;
my $var;
print defined $var ? "\$var has a value of '$var'\n" : "\$var is not defined\n";
$var = 'some val';
print defined $var ? "\$var has a value of '$var'\n" : "\$var is not defined\n";
super_gunda
08-01-2007, 06:37 PM
What if I have my code like this?
How do i tell which var is not defined?
use strict;
use warnings;
my $var123,$sdf,$a123;
IsDefined($var123,$sdf,$a123);
sub IsDefined
{
my @varList = @_;
foreach my $var (@varList)
{
unless(defined($var)){
print defined $var ? "\$var has a value of '$var'\n" : "\$var is not defined\n";
}
}
return "pass";
}
FishMonger
08-01-2007, 07:43 PM
It sounds like you're asking how to use symbolic references, which is not a good idea. To find out why read this perldoc
perldoc -q 'variable as a variable name'
You really should be using a hash.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.