in reply to Real private methods using lexical subs in Perl 5.18.

> I'll also point out that a similar thing can be achieved using coderefs in earlier versions of Perl:

> my $private_method = sub { ...; }; $self->$private_method(@args);

still my preferred way to do it and I wonder why this approach is widely ignored.

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: Real private methods using lexical subs in Perl 5.18.
by Arunbear (Prior) on Jun 01, 2013 at 11:35 UTC
    One reason is that
    my $private_method = sub { get_logger()->info('testing 1 2 3'); };
    wouldn't be able to report the method name.
      Unless Named anonymous subs helps you.
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Yes, there are workarounds available (there's also Sub::Name), but the extra effort involved is one of the reasons I suspect most folks don't use this mechanism for private methods.
        I'd rather inspect the caller for file and line to put it in the log.

        edit

        or using PadWalker to find the variable-name 2 levels up holding the coderef 1 level up.

        Cheers Rolf

        ( addicted to the Perl Programming Language)