Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Real private methods using lexical subs in Perl 5.18.

by Arunbear (Parson)
on Jun 01, 2013 at 11:35 UTC ( #1036422=note: print w/ replies, xml ) Need Help??


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

One reason is that

my $private_method = sub { get_logger()->info('testing 1 2 3'); };
wouldn't be able to report the method name.


Comment on Re^2: Real private methods using lexical subs in Perl 5.18.
Download Code
Re^3: Real private methods using lexical subs in Perl 5.18.
by choroba (Abbot) on Jun 01, 2013 at 14:19 UTC
    Unless Named anonymous subs helps you.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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)

      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.
        Really? I think most either don't know or prefer avoiding code_refs.

        It's a very special magic which is practically not mentioned in the docs and never in conjunction with the word "private".

        I grepped thru all pods for ->$ and found only 10 occurences from which 8 are only of the string form ->$methname and only 2 with ->$coderef:

        /usr/share/perl/5.10/pod/perlbot.pod: $self->{'delegate'}->$ +AUTOLOAD(@_); /usr/share/perl/5.10/pod/perlfaq7.pod: $widget->$tric +k(); /usr/share/perl/5.10/pod/perlsec.pod: $obj->$method(@args); /usr/share/perl/5.10/pod/perltooc.pod: $self->$methname($newvalue +); /usr/share/perl/5.10/pod/perltooc.pod: $self->$coderef($newvalue) +; /usr/share/perl/5.10/pod/perltoot.pod: return ref_to($c +lass->$methname); /usr/share/perl/5.10/pod/perltoot.pod: return ref_to($c +lass->$methname); /usr/share/perl/5.10/pod/perlunicode.pod: return $sth->$what; /usr/share/perl/5.10/pod/perlunicode.pod: my @arr = $sth->$what +; /usr/share/perl/5.10/pod/perlunicode.pod: my $ret = $sth->$what +;

        the only explanation I found was in perlop

        The Arrow Operator

        ""->"" is an infix dereference operator, just as it is in C and C++. If the right side is either a "...", "{...}", or a "(...)" subscript, then the left side must be either a hard or symbolic reference to an array, a hash, or a subroutine respectively. (Or technically speaking, a location capable of holding a hard reference, if its an array or hash reference being used for assignment.) See perlreftut and perlref.

        Otherwise, the right side is a method name or a simple scalar variable containing either the method name or a subroutine reference, and the left side must be either an object (a blessed reference) or a class name (that is, a package name). See perlobj.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-10-21 00:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (94 votes), past polls