doubt regarding Symbolic references

R

Rajesh

Lets Consider a simple program like this

$i = 'a';
$a = "why is it so boring?";
print "$$i";

Now, the output will be the string "why is it so boring?" because $$i
will now get referenced to $a and hence the string.

Now, instead $i has the value $a . Something like,

$i = '$a';
$a = "why is it so boring?";
print "$i";

In this case, the output will be "$a" and not the string that is
expected.
But, I want the output to be the string. How would I get the value of a
variable if another variable has this variable?
 
B

Brian Wakem

Rajesh said:
Lets Consider a simple program like this

$i = 'a';
$a = "why is it so boring?";
print "$$i";

Now, the output will be the string "why is it so boring?" because $$i
will now get referenced to $a and hence the string.

Now, instead $i has the value $a . Something like,

$i = '$a';
$a = "why is it so boring?";
print "$i";

In this case, the output will be "$a" and not the string that is
expected.
But, I want the output to be the string. How would I get the value of a
variable if another variable has this variable?


You could use:-

print eval $i;
 
P

Paul Lalli

Rajesh said:
Lets Consider a simple program like this

$i = 'a';
$a = "why is it so boring?";
print "$$i";

please read:
perldoc -q quoting
What's wrong with always quoting $vars.
Now, the output will be the string "why is it so boring?" because $$i
will now get referenced to $a and hence the string.

Now, instead $i has the value $a . Something like,

$i = '$a';
$a = "why is it so boring?";
print "$i";

In this case, the output will be "$a" and not the string that is
expected.
But, I want the output to be the string. How would I get the value of a
variable if another variable has this variable?

Another responder already told you about eval(). I will give you the
standard warning - YOU DON'T WANT TO DO THIS! Symbolic references are
bad, evil, and should not be used unless absolutely necessary. They
almost never are. The correct answer is to use a hash:

$i = 'a';
$vars{a} = 'Why is it so boring?';
print $vars{$i};

For a more thorough discussion on why you should not be using symrefs,
please consult the FAQ:

perldoc -q "variable name"

Paul Lalli
 
C

Chris Mattern

Rajesh said:
Lets Consider a simple program like this

$i = 'a';
$a = "why is it so boring?";
print "$$i";

Now, the output will be the string "why is it so boring?" because $$i
will now get referenced to $a and hence the string.

Yes, it will. Don't do that.
Now, instead $i has the value $a . Something like,

$i = '$a';
$a = "why is it so boring?";
print "$i";

In this case, the output will be "$a" and not the string that is
expected.
But, I want the output to be the string. How would I get the value of a
variable if another variable has this variable?

Don't. Use a hash.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top