Leser: 3
3 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
use auto;
use Data::Dumper;
my $test = auto->new();
print "Vor A: ".Dumper($test);
$test->A();
print "Nach A: ".Dumper($test);
$test->A_val();
print "Nach A_val: ".Dumper($test);
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
42
43
44
45
46
package auto;
sub new {
my $invocation = shift();
my $class = ref($invocation) || $invocation;
my $self = {
A => 1,
B => 2
};
my $bless = bless($self, $class);
return $bless;
}
sub DESTROY {};
sub AUTOLOAD {
my $self = shift();
our $AUTOLOAD;
for($AUTOLOAD) {
/.*::(.*)/;
$key = $1;
}
if (grep {/^$key$/} keys %{$self->{$key}}) {
if (scalar(@_)) {
$self->{$key} = shift();
} else {
return $self->{$key};
}
} else {
for($key) {
/(.*)_val/ and do {
if(grep{/$1/} keys(%{$self})) {
if (scalar(@_)) {
$self->{$1} =(shift());
} else {
return $self->{$1};
}
}
last;
};
}
}
}
1;
Quote
$ perl test.pl
Vor A: $VAR1 = bless( {
'A' => 1,
'B' => 2
}, 'auto' );
Nach A: $VAR1 = bless( {
'A' => 1,
'B' => 2
}, 'auto' );
Nach A_val: $VAR1 = bless( {
'A' => 1,
'B' => 2,
'A_val' => {}
}, 'auto' );
if (grep {/^$key$/} keys %{$self->{$key}}) {
3 Einträge, 1 Seite |