Using a string as a variable name.

J

Jeff

Hi,

I want to do things like this:

my $v1 = "varname";
my $varname = "it worked!";
print ${$v1};

And have the final statement know that it should print the content of $varname.

Is it possible?

Thanks,


Jeff
 
J

Jürgen Exner

Jeff said:
I want to do things like this:

No, you don't. Please read the FAQ (perldoc -q "variable name") and
gazillions of previous articles why this is a _very_ bad idea.
my $v1 = "varname";
my $varname = "it worked!";
print ${$v1};

And have the final statement know that it should print the content of
$varname.

Is it possible?

Technically yes. But there is no good reason to use symbolic references
unless you do want to modify the system symbol table.

Just use a hash instead. Much easier to use, to maintain, and without all
the problems associated with symbolic references.

jue
 
P

Paul Lalli

Jeff said:
Hi,

I want to do things like this:

my $v1 = "varname";
my $varname = "it worked!";
print ${$v1};

And have the final statement know that it should print the content of $varname.

Is it possible?

The standard response is "You don't want to do that. Read perldoc -q
"variable name" to understand why.

However, to answer the question you actually asked, yes, you are
technically able to do that - provided that
1) the variables are package variables, not lexicals (in your example
program, you'd have to change my to our, or disable strict vars (which
you also don't want to do))
2) strict refs has also been disabled (which yet again, is something
you don't want to do.)

In other words, "DON'T DO THIS". Use a hash. Read perldoc -q "variable
name"

Paul Lalli
 
T

Tore Aursand

I want to do things like this:

No. You _think_ you want this, but actually you don't. Please refer to
'perldoc -q "variable name"' for more information on why you should avoid
this.
my $v1 = "varname";
my $varname = "it worked!";
print ${$v1};

Better off using a hash;

my %variables = ('v1' => 'it',
'v2' => 'worked');

print "$variables{v1} $variables{v2}\n";
 
C

Chris Mattern

Jeff said:
Hi,

I want to do things like this:

my $v1 = "varname";
my $varname = "it worked!";
print ${$v1};

And have the final statement know that it should print the content of
$varname.

Is it possible?
You want to use a hash.

my %hashname;
$hashname{'keyname'}="it worked!";
my $v1="keyname";
print $hashname{$v1};

--
Christopher Mattern

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

Chris Mattern

Abigail said:
Jeff ([email protected]) wrote on MMMMLIII September MCMXCIII in
<URL:`' Hi,
`'
`' I want to do things like this:
`'
`' my $v1 = "varname";
`' my $varname = "it worked!";
`' print ${$v1};
`'
`' And have the final statement know that it should print the content of
`' $varname.

Oh, I got to ask: why do you want this? Would you want to do such a
thing in C or in Java as well?
No, but a lot of people come to Perl from shell scripting, and doing
this sort of thing in shell scripts is very common when what you
really want is a Perl-type hash. Since you generally don't have
hashes in shell scripting, you use this kind of hack instead, and
some people try to continue doing it in Perl even though there's
something 254.32 times better.

--
Christopher Mattern

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

David K. Wall

Chris Mattern said:
No, but a lot of people come to Perl from shell scripting, and
doing this sort of thing in shell scripts is very common when what
you really want is a Perl-type hash. Since you generally don't
have hashes in shell scripting, you use this kind of hack instead,
and some people try to continue doing it in Perl even though
there's something 254.32 times better.

I used to do the same sort of thing when writing SAS macros. (For
those unfamiliar with it, SAS is a statistical package, or at least
started as one.) SAS macro language has string variables only, so
even arrays have to be simulated using symbolic references.

When I started programming in Perl I applied some of the same
techniques I had used to get around the shortcomings of the SAS macro
language. When it was pointed out to me that symbolic references were
a bad idea, and more importantly, WHY they were a bad idea, I never
used them again. (at least in Perl)

But when I use SAS, I have no choice: it's symbolic references or
nothing.
 
A

A. Sinan Unur

I used to do the same sort of thing when writing SAS macros. (For
those unfamiliar with it, SAS is a statistical package, or at least
started as one.) SAS macro language has string variables only, so
even arrays have to be simulated using symbolic references. ....

But when I use SAS, I have no choice: it's symbolic references or
nothing.

I will not touch SAS macros with a ten-foot pole. It is impossible to
decipher what exactly is going on in each step and it is even more
impossible to figure out what other people are doing.

Some time ago, I used to use gslgen (http://www.imatix.com/html/gslgen/)
which would let me come up with a meta-specification in XML for what I
wanted to do and then translate into SAS or Stata code. That is how much I
hate SAS macro programming.

To bring this post somewhat near topicality, Perl makes it even easier for
me to do this. (And, no, no one has to decipher my XML stuff. I just end up
with clean SAS files that might appear a little repetitive but are
straightforward to understand even by novices).

Sorry, couldn't resist.

Sinan.
 
E

Eric J. Roode

(e-mail address removed) (Jeff) wrote in
Hi,

I want to do things like this:

my $v1 = "varname";
my $varname = "it worked!";
print ${$v1};

And have the final statement know that it should print the content of
$varname.

Is it possible?

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

${$v1} = "it worked!";

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
U

Uri Guttman

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!' ;

uri
 
B

Ben Morrow

Quoth Dan Jones said:
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
 
U

Uri Guttman

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
 
J

Jürgen Exner

Dan said:
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=symbolic ref&as_ugroup=comp.lang.perl.misc
http://www.google.com/groups?as_epq=symref&as_ugroup=comp.lang.perl.misc

jue
 
D

Dan Jones

Uri said:
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.
 
E

Eric J. Roode

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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
E

Eric J. Roode

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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
U

Uri Guttman

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
 
U

Uri Guttman

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> 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
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top