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
In reply to Re: What's so wrong with this (dereferencing)code? (Symbolic Refs)
by LanX
in thread What's so wrong with this (dereferencing)code?
by Maelstrom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |