Keep It Simple, Stupid | |
PerlMonks |
Re: Referencing the localsby Chuma (Scribe) |
on Apr 29, 2021 at 00:30 UTC ( [id://11131835]=note: print w/replies, xml ) | Need Help?? |
Thank you all for your answers! It's fair to say this is all firmly in the chaotic evil school of programming, so it's no wonder "strict" doesn't like it. Using explicit references would work in this example, but then I can't do anything else with the string. A hash would allow that, although I probably don't want to refer to the array as @{$animals{'cat'}} for the rest of the program, so I guess I could add
There's also the option of just repeating the whole code block three times over instead of looping. I suppose that's alright, but doesn't really strike me as beautiful code. The text about "why it's stupid to use a variable as a variable name" makes a good point, generally. Symbolic references can lead to uncontrolled global variables – if $x turns out to be something unexpected, there's no telling what $$x might destroy. But that's not really applicable here, firstly because the variable takes on exactly these three values, and secondly because I'm trying to make the variables not global. It seems like an odd design choice that that's apparently impossible, but maybe it's just an implementation artefact.
In Section
Seekers of Perl Wisdom
|
|