in reply to What is this doing, and why is this a good method of doing it?

It is producing a named subroutine for each name in @x. Even if somefunction() returns names which would be illegal for a sub, a function of the name will be provided.

use strict; use warnings; sub somefunction { qw/foo bar 123/ } my @x = somefunction(); for (@x) { my $foo = "${_}_func"; *{;no strict 'refs'; \*$_} = sub { $foo }; } print &123(),$/; # omitting the & sigil gives syntax error __END__ 123_func

The tight scope of no strict 'refs'; is remarkable. I've never seen it done that way. I think it is the primary advantage of doing this: stricture in in force in the sub definition. The construction seems to be equivalent to,

*{ +do { no strict 'refs'; \*$_ } } = sub { # . . . };

After Compline,

Replies are listed 'Best First'.
Re^2: What is this doing, and why is this a good method of doing it?
by japhy (Canon) on Jul 25, 2005 at 02:13 UTC
    Indeed. The function is defined with whatever level of strictures were in effect for the preceding code. Obviously, the code could be written in nicer ways, but the idea is to make sure that (specifically) no symbolic references are used in the code reference.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart