|
Posted by Uri Guttman on April 8, 2008, 1:21 pm
Please log in for more thread options
>> On Mon, 07 Apr 2008 20:55:32 +0000, Uri Guttman wrote:
>> > it is just a wacko way to define a sub. it puts a code ref (the anon
>> > sub) into the code slot in the *myname typeglob. it makes no sense to do
>> > that unless the sub name was generated on the fly.
>> I beg to differ. I do this frequently to populate a class with a
>> number of similar methods, e.g.:
>>
>> use Log::Log4Perl;
>> BEGIN {
>> for my $method ( keys %Log::Log4perl::Level::PRIORITY ) {
>> no strict 'refs';
>> *$method = sub {
>> my $self = shift;
>> my $logger = Log::Log4Perl->get_logger( ref $self );
>> $logger->$method( @_ );
>> }
>> };
>>
>> }
R> Well, your case *does* make sense, because $method is a different
R> string on every execution. My original example, however, was that
R> the method name was "constant" (i.e. set to the same string on every
R> execution), so I agree with Uri that this could have done more
R> naturally
R> in the conventional way of defineing a sub.
note that these aren't strings being compiled but anon subs. and the
only way it makes sense is if they are closures which hold private
versions of lexicals. this way each pass through the loop generates a
slightly different customized closure (in this case the name of the
method to call is in $method and private to each closure).
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
|
|
Posted by Ronny on April 9, 2008, 3:20 am
Please log in for more thread options
> >> use Log::Log4Perl;
> >> BEGIN {
> >> for my $method ( keys %Log::Log4perl::Level::PRIORITY ) {
> >> no strict 'refs';
> >> *$method = sub {
> >> my $self = shift;
> >> my $logger = Log::Log4Perl->get_logger( ref $self );
> >> $logger->$method( @_ );
> >> }
> >> };
> >>
> >> }
>
> R> Well, your case *does* make sense, because $method is a different
> R> string on every execution. My original example, however, was that
> R> the method name was "constant" (i.e. set to the same string on every
> R> execution), so I agree with Uri that this could have done more
> R> naturally
> R> in the conventional way of defineing a sub.
>
> note that these aren't strings being compiled but anon subs.
I meant: The variable $method holds a string (the name of
a priority level - for example "WARNING"), and the assignment
*$method = sub { ... }
sets the sub slot of the symbol table entry "WARNING" to the anonymous
sub, which effectively results in a sub &WARNING to be created.
Am I wrong here?
Ronald
|
|
Posted by groditi@gmail.com on April 9, 2008, 10:03 pm
Please log in for more thread options
> Well, your case *does* make sense, because $method is a different
> string on every execution. My original example, however, was that
> the method name was "constant" (i.e. set to the same string on every
> execution), so I agree with Uri that this could have done more
> naturally
> in the conventional way of defineing a sub.
>
Could the original author have done this in an attempt to evade sub
naming? perldoc Sub::Name for more info
|
|
Posted by Ronny on April 10, 2008, 4:45 am
Please log in for more thread options > Could the original author have done this in an attempt to evade sub
> naming? perldoc Sub::Name for more info
No, I found now that it's mainly an alternate way to make a closure.
I think this was not necessary here, because the definitions where
on file scope (hope this is the correct term?), and this could also
have been done by just defining the sub in the conventional way.
Anyway, I checked for Sub::Name, but there is no perldoc for it (I'm
using ActiveState Perl on Windows, release 5.10.0; I guess it isn't
in the standard distribution, but I found it on CPAN. Interesting
module, though I wonder whether I will ever find some application for
it...
Ronald
|
|
Posted by Uri Guttman on April 8, 2008, 1:18 pm
Please log in for more thread options
PS> On Mon, 07 Apr 2008 20:55:32 +0000, Uri Guttman wrote:
>>
R> I found in a program a piece of code which basically looked like this:
R> my $n="myname";
R> *$n=sub { .... };
>>
R> The whole think was of course guided by no strict 'refs'. My question:
>>
R> Is this just a unusual way to write
>>
R> sub myname { ... }
>>
R> or did I miss something here?
>>
>> it is just a wacko way to define a sub. it puts a code ref (the anon
>> sub) into the code slot in the *myname typeglob. it makes no sense to do
>> that unless the sub name was generated on the fly. and even that makes
>> little sense unless he has variant subs for the same name. and that is
>> better done with a dispatch table than munging the symbol table. i would
>> stay away from that code and coder if i were you!
PS> I beg to differ. I do this frequently to populate a class with a
PS> number of similar methods, e.g.:
PS> use Log::Log4Perl;
PS> BEGIN {
PS> for my $method ( keys %Log::Log4perl::Level::PRIORITY ) {
PS> no strict 'refs';
PS> *$method = sub {
PS> my $self = shift;
PS> my $logger = Log::Log4Perl->get_logger( ref $self );
PS> $logger->$method( @_ );
PS> }
PS> };
PS> }
i have done similar things when i autogenerated accessors. i did say
autogenerate is ok in my comment but i forgot about this case.
PS> I don't think a dispatch table is a better solution for those cases.
ditto for autoload. again guilty of doing that.
but the example from the OP was neither of those (unless there was
missing code to show that).
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
|
| Similar Threads | Posted | | Unusual warning - what's it trying tell me? | August 22, 2005, 9:40 am |
| unusual behaviour of perl script compiled with perlapp on hp-ux11i | May 8, 2005, 7:52 am |
| where to define this array? | May 15, 2005, 10:52 pm |
| Define my own signal. | May 12, 2006, 4:26 am |
| #define-like feature in Perl | September 29, 2004, 6:55 am |
| define an array in perl | July 31, 2007, 6:43 am |
| When do I need to define a function with "use POSIX qw(.....)" and when not ? | December 12, 2007, 6:45 am |
| FAQ 4.74 How do I define methods for every class/object? | February 23, 2005, 6:03 pm |
| cgi.pm popup_menu define select width? | March 27, 2005, 12:28 pm |
| FAQ 4.74 How do I define methods for every class/object? | April 26, 2005, 11:03 pm |
|