Thread private/public subs in modules (14 answers)
Opened by burnersk at 2008-12-13 16:01

LanX-
 2008-12-13 17:17
#117172 #117172
User since
2008-07-15
1000 Artikel
BenutzerIn

user image
Linuxer+2008-12-13 16:10:39--
BTW: Soll es schon mal vorkommen, dass zwei User gleichzeitig eine Antwort tippen und dass sogar beide sehr ähnlichen Inhalt haben!

das war nur zur erläuterung gedacht, außerdem nehme ichs zurück, den Code verstehe ich nicht 100%

den folgenden Text hab ich NATÜRLICH aus dem Buch abgetippt ... ; )
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# An intriguing aspect of this behavior is that it can be used to implement private method calls. 
# If you put your class in a module, you can make use of the file's lexical scope for privacy. 
# First, store an anonymous subroutine in a file-scoped lexical:

    # declare private method
    my $secret_door = sub {
        my $self = shift;
        ...
    };

# Later on in the file, you can use that variable as though it held a method name. The closure 
# will be called directly, without regard to inheritance. As with any other method, the invocant is 
# passed as an extra argument.

    sub knock {
        my $self = shift;
        if ($self->{knocked}++ > 5) {
            $self->$secret_door();
        }
    }


mit welcher Magie $self->$secret_door(); funzt soll mir bitte jmd erklären. Mein (kopf-)schmerzendes Hirn kann nur vermutungen über AUTOLOAD anstellen... oder haben wir hier ein mir unbekanntes Feature von -> ?
Antworten mit Zitat

View full thread private/public subs in modules