Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Self-writing code revisited

by kennethk (Monsignor)
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.


Comment on Re: Self-writing code revisited
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1052639]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-11-26 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (162 votes), past polls