variable evaluation and printing

M

manunderstress

in the code below, why will it print (and add to the hash) $status but
not $ifconfig? Also when $status = "Production"; it fails to print or
add to hash as well.

something about variable evaluation/interpretation i'm not
understanding?

#ifconfig command on a solaris box; outputs multiple lines of text
my $ifconfig=`ifconfig -a`;
my $status='Production';

my @params = qw(status ifconfig);
foreach $item (@params) {

if (defined $item) {
print "item name: $item item value:". ${$item}. "\n";
$post_checks{"$item"} = "${$item}";
}
}
 
J

John W. Krahn

manunderstress said:
in the code below, why will it print (and add to the hash) $status but
not $ifconfig? Also when $status = "Production"; it fails to print or
add to hash as well.

something about variable evaluation/interpretation i'm not
understanding?

#ifconfig command on a solaris box; outputs multiple lines of text
my $ifconfig=`ifconfig -a`;
my $status='Production';

my @params = qw(status ifconfig);
foreach $item (@params) {

if (defined $item) {
print "item name: $item item value:". ${$item}. "\n";
$post_checks{"$item"} = "${$item}";
}
}


It looks like you want to do this instead:


my %post_checks = (
status => 'Production',
ifconfig => scalar `ifconfig -a`,
);



(The answer to your question is in: perldoc -q "How can I use a variable as a
variable name"; but don't do that.)



John
 
M

manunderstress

You are trying to use symbolic references, and have demonstrated one
reason they are not recommended (they only work on global variables and
your variables are lexical).

See 'perldoc -q variable

How can I use a variable as a variable name?

for other reasons why not to do this. Use a hash, instead.

Fair enough, but now I'm at a loss how else to do what I want to do,
that is, without using scalar variables I have previously created in
the script as keys for the hash (%post_checks)- and their evaluation
as the value for the hash.

I have a bunch of scalar variables that will be dynamically generated
(hostid, ifconfig, whatever) and I want to add each key->value pair -
if it exists- to a hash (what I thought I was doing...by adding key-
 
P

Paul Lalli

Fair enough, but now I'm at a loss how else to do what I want to do,
that is, without using scalar variables I have previously created in
the script as keys for the hash (%post_checks)- and their evaluation
as the value for the hash.

I have a bunch of scalar variables that will be dynamically generated
(hostid, ifconfig, whatever) and I want to add each key->value pair -
if it exists- to a hash (what I thought I was doing...by adding key->values to %post_checks).

Don't dynamically generate scalar values. Dynamically generate the
key/value pairs.

You're not showing us how you're "dynamically generating" the scalar
values, so we can't help you fix that part of your program. . .

Paul Lalli
 
B

Brian McCauley

Fair enough, but now I'm at a loss how else to do what I want to do,
that is, without using scalar variables I have previously created in
the script as keys for the hash (%post_checks)- and their evaluation
as the value for the hash.

I have a bunch of scalar variables that will be dynamically generated
(hostid, ifconfig, whatever) and I want to add each key->value pair -
if it exists- to a hash (what I thought I was doing...by adding key-
values to %post_checks).

And therein is the problem. Why is it that you are attached to the
idea that you want "a bunch of [named] scalar variables that will be
dynamically generated"? This is precisely what people here (and the
FAQ) are advising against (at least as a general rule). The way to
have a "bunch" of scalars in Perl is (in general) to make them into a
hash in the first place.

If you are really determined to go with "a bunch of [named] scalar
variables that will be dynamically generated" then you have no choice
but to use symrefs (or their even more evil cousin, eval()).

Can you perhaps explain what it is that you expect to gain from using
"a bunch of [named] scalar variables" rather than a hash?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top