chinaxing has asked for the wisdom of the Perl Monks concerning the following question:

I reading programming perl, at chapter 12, the objects, has following documents :

The next::method has a similar problems because it uses the package of its
caller to figure out what class to look at. If you define a method in Donkey from
another package, next::method will break:

package main; *Donkey::sound = sub { (shift)–>next::method(@_) };

The anonymous subroutine shows up in the stack with as _ _ANON_ _, so next::method doesn’t know which package it is in. You can use the Sub::Name CPAN module to make it work out, though:

use Sub::Name qw(subname); *Donkey::sound = subname 'Donkey::sound' => sub { (shift)–>next::method(@_) };

I am very inquisitive how Sub::Name module complete this work ? let the anonymous subroutine's caller not be __ANON__

very thanks your help :)

Replies are listed 'Best First'.
Re: how Sub::Name set anonymous subroutine 's caller to NOT __ANON__ ?
by tobyink (Canon) on Jul 15, 2013 at 09:40 UTC

    The CV structure used to represent coderefs internally in Perl's guts has slots for the sub's name and stash (package). This information is used by Carp when generating stack traces. Sub::Name uses some XS to poke some strings into those slots.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: how Sub::Name set anonymous subroutine 's caller to NOT __ANON__ ? (__PACKAGE__)
by Anonymous Monk on Jul 15, 2013 at 09:56 UTC