Disguising the fact that you are doing something that isn't strict-safe by finding a way to do with with strict on is only fooling yourself and your readers. And that's a bad idea.
First of all, it's not very common. It's done by a few low-level modules. The normal Perl programmer will never manipulate the symbol table directly.
And yes, it is possible to do this without disabling strict
. The symbol table is accessible via %::
, so you could parse the package and navigate the symbol table that way. For example, \%{'Foo::Bar::'}
becomes \%{ $::{"Foo::"}{"Bar::"} }
, although that presupposes the existence of the package. You could also generate Perl code and use eval
.
BUT, there's no reason to do so. One turns on strict specifically to prevent oneself from accidentally doing this. Disguising the fact that you are doing something that isn't strict-safe is only fooling yourself and your readers. And that's a bad idea. One should strive to write self-documenting code, and nothing says "this is tricky, dangerous code" like no strict 'refs';
.