E
Eric Pement
I have a question that's really bugging me. I've tried looking in the
Perl FAQ, the Perl Cookbook, the White Camel, and the Perl How-to
under words like scalars, variables, names, reference, and
dereference. No luck. (Or if the answer was present, I didn't
recognize it.)
What I'd like to do is something like this, where foreach() is
followed by a list of scalars:
$v1 = "foo";
$v2 = "bar";
$v3 = "baz";
foreach my $arg ($v1, $v2, $v3) {
print "name: \\$arg, value: $arg\n";
}
Desired output:
name: $v1, value: foo
name: $v2, value: bar
name: $v3, value: baz
Obviously, \\$arg won't work, and I've tried 15 other constructions
and ways of building the foreach loop, all without success. What's the
ticket here? How do I retrieve the name of the variable instead of its
value?
Now I expect that someone will suggest that I build a hash and then
run "foreach(sort keys %hash)" to retrieve name-value pairs. That's
not perfectly workable for me. What I'm actually doing is debugging
someone else's CGI script of several thousand uncommented lines, and
at key points I need to access the simple scalars that are already
present rather than rewriting everything. In point of fact, the
application calls over a dozen perl scripts and setting files, and I
don't have the time or the mandate to overhaul the entire script. What
I need are some well-placed diagnostics which will be used today and
erased tomorrow, and the answer to this question would really help.
Thanks in advance.
Perl FAQ, the Perl Cookbook, the White Camel, and the Perl How-to
under words like scalars, variables, names, reference, and
dereference. No luck. (Or if the answer was present, I didn't
recognize it.)
What I'd like to do is something like this, where foreach() is
followed by a list of scalars:
$v1 = "foo";
$v2 = "bar";
$v3 = "baz";
foreach my $arg ($v1, $v2, $v3) {
print "name: \\$arg, value: $arg\n";
}
Desired output:
name: $v1, value: foo
name: $v2, value: bar
name: $v3, value: baz
Obviously, \\$arg won't work, and I've tried 15 other constructions
and ways of building the foreach loop, all without success. What's the
ticket here? How do I retrieve the name of the variable instead of its
value?
Now I expect that someone will suggest that I build a hash and then
run "foreach(sort keys %hash)" to retrieve name-value pairs. That's
not perfectly workable for me. What I'm actually doing is debugging
someone else's CGI script of several thousand uncommented lines, and
at key points I need to access the simple scalars that are already
present rather than rewriting everything. In point of fact, the
application calls over a dozen perl scripts and setting files, and I
don't have the time or the mandate to overhaul the entire script. What
I need are some well-placed diagnostics which will be used today and
erased tomorrow, and the answer to this question would really help.
Thanks in advance.