Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Self-writing code revisited

by kennethk (Abbot)
on Sep 05, 2013 at 23:44 UTC ( #1052639=note: print w/replies, xml ) Need Help??


in reply to Self-writing code revisited

I know you can do something like this with hashes, but I don't want to use hashes.
Requisite warnings: Why it's stupid to use a variable as a variable name. Seriously, read all three pages.

But since you asked, you are looking for Symbolic references. Your code would be written as

#!/usr/bin/perl -w use strict "vars" ; my $data ; my @splitter ; $data = "testval,1" ; @splitter = split( "," , $data ) ; ${$splitter[ 0 ]} = $splitter[ 1 ] ; print ${$splitter[ 0 ]}, "\n";

where I added a print for fun. This creates a maintenance headache, and note that your $testval variable is never associated with a declaration (my or our), so if the last line were print $testval, "\n"; you'd get a compile time error from strict 'vars'.

Also note that what I've written creates a package variable, not a lexical variable. This means you might have weird scoping issues pop up depending on what you are actually doing.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1052639]
help
Chatterbox?
LanX predicts problems for Erix next time he lines up at an US immigration counter
[erix]: but tuesdays just as bad
[erix]: thanks for the heads-up but that's a line-up studiiously avoided :P
[LanX]: yeah too far for someone who can't even make it to Amsterdam
LanX looking for a good magit tutorial
LanX ... and git in general
[erix]: I've been to Amsterdam before you were born
[LanX]: 1899?

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2017-08-18 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (302 votes). Check out past polls.

    Notices?