Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: What is this doing, and why is this a good method of doing it?

by Zaxo (Archbishop)
on Jul 25, 2005 at 02:04 UTC ( [id://477651]=note: print w/replies, xml ) Need Help??


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,
Zaxo

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://477651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found