How to "convert" a string into a variable name?

  • Thread starter Markus Hänchen
  • Start date
M

Markus Hänchen

Hi,

very simple question:

$TempOut = 25;
$name = '$' . 'Temp' . 'Out';
print $name;

What do I have to change to get '25' printed and not '$TempOut'? Or is
this not possible?
Thanks.

Markus
 
B

Ben Morrow

Quoth =?ISO-8859-1?Q?Markus_H=E4nchen?= said:
Hi,

very simple question:

$TempOut = 25;
$name = '$' . 'Temp' . 'Out';
print $name;

What do I have to change to get '25' printed and not '$TempOut'? Or is
this not possible?

This is a FAQ. perldoc -q "variable name".

Ben
 
M

Markus Hänchen

Found the solution myself:

$TempOut = 25;
$name = 'print '. '"$' . 'Temp' . 'Out"';
eval $name;

Sorry for bothering you.
Markus
 
P

Paul Lalli

Found the solution myself:

$TempOut = 25;
$name = 'print '. '"$' . 'Temp' . 'Out"';
eval $name;

While it's good that you found *a* solution on your own, I must caution
you that this is a *bad* solution. Very very bad. eval() is a powerful
but dangerous function. If at any point, the string you're eval'ing
contains input from a user, you could do some serious damage to your
program and to your system.

A *good* solution is to _not_ try to create a variable name out of a
string. Instead, store all the "variables" you want to access this way
in a hash. The keys are whatever you currently have as a variable
name, and the value is the value you wanted to assign to that variable.
Using that method, your example above becomes:

my %value_of
$value_of{TempOut} = 25;
$name = 'Temp' . 'Out';
print $value_of{$name};

This is all spelled out much more clearly in the FAQ:
perldoc -q "variable name"

I strongly suggest you read it.

Good luck,
Paul Lalli
 
M

Markus Hänchen

While it's good that you found *a* solution on your own, I must caution
you that this is a *bad* solution. Very very bad. eval() is a powerful
but dangerous function. If at any point, the string you're eval'ing
contains input from a user, you could do some serious damage to your
program and to your system.

A *good* solution is to _not_ try to create a variable name out of a
string. Instead, store all the "variables" you want to access this way
in a hash. The keys are whatever you currently have as a variable
name, and the value is the value you wanted to assign to that variable.
Using that method, your example above becomes:

my %value_of
$value_of{TempOut} = 25;
$name = 'Temp' . 'Out';
print $value_of{$name};

Thanks Paul. I really should use a hash (not least since my eval string
gets user input...).
 
T

thrill5

I think this is what you had in mind:

$TempOut = 25;
$name = "TempOut";
print $$name;

Scott
 
J

Jürgen Exner

thrill5 said:
I think this is what you had in mind:
print $$name;

Recommending or showing someone how to use symbolic references without
pointing out the risks at the same time is quite reckless to say the least.

jue
 
P

perlistpaul

You shouldn't practice the use of symbolic reference, if you use under
'strict' it will complain.

Why not using symbolic reference vs. soft reference.

You check yourself why not the use it or what reason?

Phal
 
U

Uri Guttman

p> You shouldn't practice the use of symbolic reference, if you use under
p> 'strict' it will complain.

p> Why not using symbolic reference vs. soft reference.

those are names for the same thing, so why the vs?

p> You check yourself why not the use it or what reason?

obviously english is not your primary language but i can't figure out
what you mean there.

uri
 
A

Anonymous

This message group is NOT a programming class, it is a forum where people
can ask questions about Perl and others can HELP them. Just because YOU
don't think it is good programming practice is no reason not to answer the
OP's question. Your idea of help is to berate and bully posters, lecture on
what you think is good practice, what you think is not good practice, how to
post, read the FAQ, read the docs, etc., etc. This is the most arrogant
newsgroup on the 'net, and your presence here is the number one contributor
to that attitude. Quite frankly this newsgroup would be a much better and
NICER place without you.

Scott
 
U

Uri Guttman

A> This message group is NOT a programming class, it is a forum where
A> people can ask questions about Perl and others can HELP them. Just
A> because YOU don't think it is good programming practice is no
A> reason not to answer the OP's question. Your idea of help is to
A> berate and bully posters, lecture on what you think is good
A> practice, what you think is not good practice, how to post, read
A> the FAQ, read the docs, etc., etc. This is the most arrogant
A> newsgroup on the 'net, and your presence here is the number one
A> contributor to that attitude. Quite frankly this newsgroup would
A> be a much better and NICER place without you.

when you start answering all those FAQ's yourself and consistantly do it
in your nicest way, then we will stop doing the job in our way. as for
the actual request to use symrefs, if you actually think telling a
newbie how to do such an evil thing is a good idea, then you must also
like showing schoolkids how to load and use machine guns. they are about
the same level of nastiness.

maybe you don't realize that symrefs are JUST USING THE SYMBOL TABLE AS
A DATA STRUCTURE. can you see that using a hash for that would be
faster, safer, cleaner, better in all possible ways? if not, you should
not be flaming anyone here but i think you know that, don't you? now if
you go away then this group will become much nicer. in fact much nicer
than if sinan went away.

and to top it off, you top posted which is against this groups
guidelines. you have read those haven't you? they advocate looking for
answers in the faq.

and as for what this group is, you have no foundation to say anything
about that until you have been answering questions on a long term basis
and others grant you the respect based on your skills. note that trolls
like robic and moronzilla don't qualify but at least they sometimes
discuss perl itself unlike your silly useless rant.

and it is not a forum or a message group. it is a usenet or netnews
group. but of course you know that since you rule here.

have a nice coding experience without help from me or most of the
regulars here. i will make sure to not hire you too.

yours sincerely,

a much bigger clpm cop than you will ever be,

uri
 
P

perlistpaul

I give suggestion, but not mean I adhesive with it.
Symbolic reference is considered dangerous
Alternative is soft reference


A. Sinan Unur said:
[ Please do not top-post. Please do not quote signatures. ]
This message group is NOT a programming class, it is a forum where
people can ask questions about Perl and others can HELP them. Just
because YOU don't think it is good programming practice is no reason
not to answer the OP's question.

The OP's question has been answered. The OP is a rational person who saw
the value of using a hash in this case, and the potential problems with
using a symref
Your idea of help is to berate and bully posters,

There is no bullying above. Just a reminder that using symrefs is not a
good idea most of the time, and a pointer to the FAQ where the reasons for
the caution are well explained
lecture on what you think is good practice, what you
think is not good practice, how to post, read the FAQ, read the docs,
etc., etc. This is the most arrogant newsgroup on the 'net, and your
presence here is the number one contributor to that attitude.

Thank you for holding me in such high esteem.
Quite frankly this newsgroup would be a much better and NICER place
without you.

Scott

I post what I feel is the right response to each post. Thoughtful
questions posted by rational people get thoughtful responses. Sometimes I
can provide a solution, sometimes I make mistakes, and I learn from
corrections. That is the benefit of this group to me: Learning by trying
to answer questions, and by reading others' corrections of my posts.

Posts made without any effort, without even checking the FAQ list, or the
documentation for functions used etc subtract from that value, and
therefore I try to discourage them.

Feel free to ignore me. However, let me at least point out that I do not
hide behind anonymous handles when I post. Come back when you actually
develop the courage back up your posts.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
P

Paul Lalli

perlistpaul said:
I give suggestion, but not mean I adhesive with it.

I have *no* idea what that means. Clearly, English is not your first
language, and that's fine. But the above is gibberish. "Adhesive"
means sticky, as in tape or glue.

Symbolic reference is considered dangerous
Yes.

Alternative is soft reference

Uh. No. "Soft reference" is a synonym for "symbolic reference". Two
different terms for the same thing.

Paul Lalli
 
U

Uri Guttman

PL> I have *no* idea what that means. Clearly, English is not your first
PL> language, and that's fine. But the above is gibberish. "Adhesive"
PL> means sticky, as in tape or glue.


PL> Yes.

PL> Uh. No. "Soft reference" is a synonym for "symbolic reference". Two
PL> different terms for the same thing.

i told him the same thing earlier in this thread. he doesn't seem to
read or comprehend our responses.

uri
 
A

Anonymous

Yes, I have read the posting guidelines and I'm top posting anyway, so
there!

There is nothing wrong with showing a school kid how to use and load a
machine gun, just as there is nothing wrong with telling a newbie how to use
symbolic references. On the other hand, giving a school kid a loaded
machine gun would be very irresponsible, but what is the worst thing a
person could do with symrefs? Oh my god, they could crash their
program!!!!!! And where was it established that the question was posted by
a newbie and why should I care? I didn't know that certain information was
only to be disclosed to other members of the elite and I don't remember
signing a non-disclosure. What a minute, does that mean I haven't been
inducted to the elite and I'm not suppose to have that information either?
Is that why you were so incensed that I posted without my e-mail address so
the thought police, aka clpm cops, can track me down and throw me into the
gulag? The best way to learn is to try something new and see what happens,
this is the way newbies turn into experts.

I have been reading and posting to this newsgroup for many years. The main
reason I read the newsgroup is to learn more about Perl, and reading
questions and answers in the newsgroup furthers that. I don't post often
because:
a) I have a real job and a life and I only peruse the newsgroups every few
days, and sometimes not for weeks.
b) If I don't have anything CONSTRUCTIVE to add, I don't post.

Berating and bulling posters discourages others from posting questions.
Stupidity is in the eye of the beholder, but then again I'm sure people go
out of their way to post stupid questions so they can be belittled by the
likes of you an Sinan.

I have never posted a question here, and probably never will. I know how to
read the FAQs, and know how to read a book, and just about everything you
need to know about Perl is published somewhere. The questions I would ask,
I would be told to read the FAQ anyway, so what's the point? Yeah, the
amount of time it would take to answer the question would the same or less
than telling me to read the FAQ, but I would be told that anyway. I would
LIKE to be able to post simple questions because someone here could probably
answer it in 2 minutes or less, while it might take me much longer than that
to track down the correct documentation to read. It would be a tragedy to
post an answer to a question that someone could read in the documentation.

The posting guidelines are just that GUIDELINES. There are no rules here.
If you want rules and want them followed, go troll comp.lang.perl.moderated.

I don't currently work as a professional programmer and don't have any plans
to do so, but it still hurts that you wouldn't recommend me. I'm so hurt
I'm seriously considering rewriting my current Perl project to use symrefs
and killing my program. You have also put me in my place and proven to me I
don't rule here because I called this a "forum and newsgroup" instead of "a
usenet or netnews". You really do RULE here big comp.lang.perl.misc cop
dude!!!!!

Making you sound stupid sure is a lot of fun, maybe there is something to
being a Usenet bully. It's not very constructive, or nice and I once read
"it's nice to be important, but it's more important to be nice." I wonder
where I could have read that....

Scott

Ooopps, I mean "anonymous"
 

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,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top