in reply to Re: Moose and caller() for current method
in thread Moose and caller() for current method

Thanks.
The question is: I could hardcode the method name in the code below instead of using caller, or perhaps Moose offers an easier way to determine the method name being executed. The reasons are the same anyone would need caller in the first place. Maybe I'm looking at this from the caller's perpective and it is the called object's method the one who should introspect the caller's object method?
sub hey { &waka(((caller(0))[3]=~m/::(\w+)$/)); } sub waka { my $who = shift; print "$who called waka\n"; }

Replies are listed 'Best First'.
Re^3: Moose and caller() for current method
by BrowserUk (Patriarch) on Jul 13, 2010 at 17:43 UTC

    The only good reason for doing this is error reporting, in which case Carp::cluck() does the job perfectly.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Not quite the only good reason. In building something like a simple FSM it is useful to know the action being executed to validate for certain parameters to execute that action. This could be set by the calling sub as a parameter or the called sub to introspect. I'm thinking it should be the called sub instead of what I'm trying to do, but want to use Moose's best practices.
        it is useful to know the action being executed

        Useful to whom? Cos code doesn't give a toss what it is called; or even if its caller has a name.

        If you are writing subroutines that do different things dependant upon where they are called from, you've completely missed the point of OO, encapsulation and even structured programming.

        You are violating the first principle of reusable encapsulation--Loose coupling--by building in tight dependency by design. In other words, you've re-invented spaghetti code.

        This current fad for runtime introspection is the single biggest backward step in programming in the last 30 years.

        "Modern Perl" be damned. It is nought more than another attempt to introduce programming by rote. But like painting by numbers, it is never going to produce works of art; nor even practical craft.

        It's the new "New Math". And history shows what happened to that.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.