Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using a string as a variable name.

Reply
Thread Tools

Using a string as a variable name.

 
 
Ben Morrow
Guest
Posts: n/a
 
      10-09-2004

Quoth Dan Jones <(E-Mail Removed)>:
> Uri Guttman wrote:
> >
> > strange that you should know better than to give a symref answer without
> > all the usual caveats on how dangerous and wrong it is.
> >
> > $hash{$v1} = 'better than just working!' ;

>
> Care to expound? What is dangerous about the first example? What's with
> the "hash" in the second? As I understand it, there was no hash involved -
> it was the name of a scalar variable stored as a string in another scalar.
> Is hash a keyword or simply a variable name? If this is a FAQ or a perldoc
> question, a pointer to the right location would be appreciated. I don't
> find anything looking there and a Google for $hash doesn't show up anything
> that seems relevant.


Google for the several-threads-a-week on this topic (using a variable as
a variable name, or 'symrefs'), or read perldoc -q variable name. If you
don't know what a hash is, start with perldoc perldata.

Ben

--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      10-09-2004
>>>>> "DJ" == Dan Jones <(E-Mail Removed)> writes:

DJ> Uri Guttman wrote:

>> strange that you should know better than to give a symref answer without
>> all the usual caveats on how dangerous and wrong it is.
>>
>> $hash{$v1} = 'better than just working!' ;


DJ> Care to expound? What is dangerous about the first example?
DJ> What's with the "hash" in the second? As I understand it, there
DJ> was no hash involved - it was the name of a scalar variable stored
DJ> as a string in another scalar. Is hash a keyword or simply a
DJ> variable name? If this is a FAQ or a perldoc question, a pointer
DJ> to the right location would be appreciated. I don't find anything
DJ> looking there and a Google for $hash doesn't show up anything that
DJ> seems relevant.

search google for my name and posts about symrefs or symbolic refs. i
have posted many times as to why they are wrong.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-09-2004
Dan Jones wrote:
> Uri Guttman wrote:
>> strange that you should know better than to give a symref answer
>> without all the usual caveats on how dangerous and wrong it is.
>>
>> $hash{$v1} = 'better than just working!' ;

>
> Care to expound? What is dangerous about the first example?


You must have missed the gazillions of posts about why symbolic references
are bad, bad, BAD.

Why would you want to mess around with the system symbol table (that's what
you are effectively doing with symrefs) instead of using your own hash? Not
to mention the implied limitations like local variables, strictures, etc.

> What's with the "hash" in the second? As I understand it, there was no
> hash
> involved -


Yes, there was. Symbolic references are abusing the internal system symbol
table hash.

> it was the name of a scalar variable stored as a string in
> another scalar. Is hash a keyword or simply a variable name? If this
> is a FAQ or a perldoc question, a pointer to the right location would
> be appreciated. I don't find anything looking there and a Google for
> $hash doesn't show up anything that seems relevant.


perldoc -q "variable name"
http://www.google.com/groups?as_epq=...lang.perl.misc
http://www.google.com/groups?as_epq=...lang.perl.misc

jue


 
Reply With Quote
 
Dan Jones
Guest
Posts: n/a
 
      10-09-2004
Uri Guttman wrote:

>>>>>> "EJR" == Eric J Roode <(E-Mail Removed)> writes:

>
> >> Is it possible?

>
> EJR> Strange that you could figure out how to print the indirect value
> EJR> out, but you couldn't figure out how to assign indirectly.
>
> EJR> ${$v1} = "it worked!";
>
>
> strange that you should know better than to give a symref answer without
> all the usual caveats on how dangerous and wrong it is.
>
> $hash{$v1} = 'better than just working!' ;


Care to expound? What is dangerous about the first example? What's with
the "hash" in the second? As I understand it, there was no hash involved -
it was the name of a scalar variable stored as a string in another scalar.
Is hash a keyword or simply a variable name? If this is a FAQ or a perldoc
question, a pointer to the right location would be appreciated. I don't
find anything looking there and a Google for $hash doesn't show up anything
that seems relevant.


 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-12-2004
Uri Guttman <(E-Mail Removed)> wrote in
news(E-Mail Removed):

>>>>>> "EJR" == Eric J Roode <(E-Mail Removed)> writes:

>
> >> Is it possible?

>
> EJR> Strange that you could figure out how to print the indirect value
> EJR> out, but you couldn't figure out how to assign indirectly.
>
> EJR> ${$v1} = "it worked!";
>
>
> strange that you should know better than to give a symref answer without
> all the usual caveats on how dangerous and wrong it is.
>
> $hash{$v1} = 'better than just working!' ;


Aw, Uri, you're not letting me have any fun!

Actually, I didn't feel it necessary, since others had already given good
reasons not to do so (and to read the FAQ).

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      10-12-2004
Dan Jones <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Uri Guttman wrote:
>
>>>>>>> "EJR" == Eric J Roode <(E-Mail Removed)> writes:

>>
>> >> Is it possible?

>>
>> EJR> Strange that you could figure out how to print the indirect
>> value EJR> out, but you couldn't figure out how to assign
>> indirectly.
>>
>> EJR> ${$v1} = "it worked!";
>>
>>
>> strange that you should know better than to give a symref answer
>> without all the usual caveats on how dangerous and wrong it is.
>>
>> $hash{$v1} = 'better than just working!' ;

>
> Care to expound? What is dangerous about the first example? What's
> with the "hash" in the second? As I understand it, there was no hash
> involved - it was the name of a scalar variable stored as a string in
> another scalar. Is hash a keyword or simply a variable name? If this
> is a FAQ or a perldoc question, a pointer to the right location would
> be appreciated. I don't find anything looking there and a Google for
> $hash doesn't show up anything that seems relevant.


Okay, I take it back, Uri.

Dan:
It is a FAQ; pointers to the FAQ are posted here regularly, and can
be found by a quick google search, or (iirc) at rtfm.mit.edu.

In short: using a hash instead of variable-names-as-a-variable is far
superior for several reasons. The hash is neater (it's one data
structure instead of multiple); it encapsulates the data in one neat
package, and allows it to be hidden better within a subroutine or an
object (insofar as Perl allows data to be hidden) (Encapsulation and Data
Hiding are two important concepts in modern computer science); the
variable-name approach can often lead to hard-to-find bugs due to typos
and bad logic.

No experienced Perl programmer uses variables as variable names
except in those rare situations when no other approach will work.

(Better, Uri? )
--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      10-12-2004
>>>>> "EJR" == Eric J Roode <(E-Mail Removed)> writes:

EJR> Dan Jones <(E-Mail Removed)> wrote in
EJR> news:(E-Mail Removed):

>> Uri Guttman wrote:
>>
>>>>>>>> "EJR" == Eric J Roode <(E-Mail Removed)> writes:
>>>
>>> >> Is it possible?
>>>

EJR> Strange that you could figure out how to print the indirect
>>> value EJR> out, but you couldn't figure out how to assign
>>> indirectly.
>>>

EJR> ${$v1} = "it worked!";
>>>
>>>
>>> strange that you should know better than to give a symref answer
>>> without all the usual caveats on how dangerous and wrong it is.
>>>
>>> $hash{$v1} = 'better than just working!' ;


EJR> Okay, I take it back, Uri.

EJR> Dan:
EJR> It is a FAQ; pointers to the FAQ are posted here regularly, and can
EJR> be found by a quick google search, or (iirc) at rtfm.mit.edu.

EJR> In short: using a hash instead of variable-names-as-a-variable is far
EJR> superior for several reasons. The hash is neater (it's one data
EJR> structure instead of multiple); it encapsulates the data in one neat
EJR> package, and allows it to be hidden better within a subroutine or an
EJR> object (insofar as Perl allows data to be hidden) (Encapsulation and Data
EJR> Hiding are two important concepts in modern computer science); the
EJR> variable-name approach can often lead to hard-to-find bugs due to typos
EJR> and bad logic.

EJR> No experienced Perl programmer uses variables as variable names
EJR> except in those rare situations when no other approach will work.

EJR> (Better, Uri? )

better but not perfect!

you should have hunted down all copies of your erroneous post and
deleted them and burned all the disks that even carried it.



and the rule for symrefs is simple. ONLY use symrefs when you NEED to
mung the symbol table. they have no other purpose and regular hashes
are better in every way for munging data.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      10-12-2004
>>>>> "EJR" == Eric J Roode <(E-Mail Removed)> writes:

EJR> Uri Guttman <(E-Mail Removed)> wrote in
EJR> news(E-Mail Removed):

>>>>>>> "EJR" == Eric J Roode <(E-Mail Removed)> writes:

>>
>> >> Is it possible?

>>

EJR> Strange that you could figure out how to print the indirect value
EJR> out, but you couldn't figure out how to assign indirectly.
>>

EJR> ${$v1} = "it worked!";
>>
>>
>> strange that you should know better than to give a symref answer without
>> all the usual caveats on how dangerous and wrong it is.
>>
>> $hash{$v1} = 'better than just working!' ;


EJR> Aw, Uri, you're not letting me have any fun!

EJR> Actually, I didn't feel it necessary, since others had already
EJR> given good reasons not to do so (and to read the FAQ).

but he may have read your post first. or someone may find it in
google. if strict were on by default (other than for one liners) we
wouldn't have this newbie use of symref syndrome.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Evaluate an string variable's value to a variable Mir Nazim Python 2 12-21-2008 07:05 AM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
using std::string; string("hello") vs std::string("hello") in header file. Fei Liu C++ 9 04-01-2006 08:49 AM
How to set a variable to a not modifying string expression using a variable Stefan Mueller Javascript 9 11-01-2005 09:06 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments