Leser: 1
![]() |
![]() |
4 Einträge, 1 Seite |
1 2 3 4 5 6
sub count { my $self=shift; if (@_) { $self->{count} += shift } return $self->{count}; }
1 2 3 4 5
sub count :lvalue{ my $self=shift; if (@_) { $self->{count} += shift } $self->{count}; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
package Class;
use strict;
use warnings;
use Scalar::Util qw(weaken);
my @objects;
sub new {
my ( $class ) = @_;
# Objekt erstellen
my $self = bless {}, $class;
# zum array hinzufügen
push @objects, $self;
# Wert weaken
weaken( $objects[-1] );
# Object zurück geben
return $self;
}
sub count {
my ( $self ) = @_;
return scalar @objects;
}
}
printf "Anzahl: %d\n", Class::count;
my $new1 = Class->new;
my $new2 = Class->new;
printf "Anzahl: %d\n", $new1->count;
{
my $new3 = Class->new;
printf "Anzahl: %d\n", $new3->count;
}
printf "Anzahl: %d\n", $new1->count;
![]() |
![]() |
4 Einträge, 1 Seite |