Schrift
[thread]6897[/thread]

AUTOLOAD: Verständnis Frage

Leser: 3


<< >> 3 Einträge, 1 Seite
frodus
 2005-04-16 14:20
#53844 #53844
User since
2003-09-26
147 Artikel
BenutzerIn
[default_avatar]
Hi Leute,

ich habe mal eine Verständnisfrage zu AUTOLOAD.
Und zwar habe ich eine Klasse die eine in der ich den Autoload
mechanismus benutze um set und get methoden zu realisieren.
Was mir dabei auffällt, das sich eine Methode die ich benutze
danach als Hash referenz in meinem $self Hash widerfindet ist das normal?

Hier ist mal ein Beispiel:

1. test.pl
Code: (dl )
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);


2. Classe:
Code: (dl )
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;


Und hier ist meine Ausgabe:
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' );
betterworld
 2005-04-16 16:17
#53845 #53845
User since
2003-08-21
2613 Artikel
ModeratorIn

user image
In dieser Zeile:
Code: (dl )
if (grep {/^$key$/} keys %{$self->{$key}}) {
wird $self->{$key} als Hash-Referenz angelegt. Das nennt sich Auto-Vivicatation. Du findest in den Perldocs oder bei Google mehr darueber.
frodus
 2005-04-16 16:38
#53846 #53846
User since
2003-09-26
147 Artikel
BenutzerIn
[default_avatar]
Danke ! :)
<< >> 3 Einträge, 1 Seite



View all threads created 2005-04-16 14:20.