in reply to Referencing the locals
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
my @cat = @{$animals{'cat'}}; my @dog = @{$animals{'dog'}}; my @pig = @{$animals{'pig'}};
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Referencing the locals
by haukex (Archbishop) on Apr 29, 2021 at 07:10 UTC | |
by Chuma (Scribe) on Apr 29, 2021 at 11:57 UTC | |
by haukex (Archbishop) on Apr 29, 2021 at 12:35 UTC | |
by Chuma (Scribe) on Apr 29, 2021 at 14:27 UTC | |
by haukex (Archbishop) on Apr 29, 2021 at 14:37 UTC | |
|