Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1052639]
and the sunlight beams...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2017-02-22 00:57 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (322 votes). Check out past polls.