in reply to What's so wrong with this (dereferencing)code?
In most cases it's better to just use hash values.
hash slices make this easy:
my %time; @time{ qw(sec min hour ...) } = localtime(time);
or
my %time; my @pad = qw (sec min hour day month); @time{ @pad } = localtime(time);
Please note how this is more DRY than your redundant code.
The reason why symbolic references are deactivated by default "strictness" is that they result in very hard to spot errors.°
If you really need this kind of meta programming, you can still reactivate it with no strict 'refs' within the local scope.¹
And to why it doesn't work with private lexicals, I suppose it has to do with the history of Perl 4 to 5, the former didn't have strict or lexical vars.
Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery
°) Especially people inexperienced to hashes are prone to this.
FWIW: I've seen languages where this was the only way to emulate hashes, but Perl is not one of them.
¹) though you can already do all of this with %package:: stash or PadWalker or eval constructs
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: What's so wrong with this (dereferencing)code? (Symbolic Refs)
by Maelstrom (Beadle) on Jun 26, 2024 at 12:34 UTC | |
by LanX (Saint) on Jun 26, 2024 at 13:44 UTC | |
by etj (Priest) on Jun 27, 2024 at 10:08 UTC | |
by LanX (Saint) on Jun 27, 2024 at 13:32 UTC | |
by etj (Priest) on Jun 27, 2024 at 13:38 UTC | |
|