Thread OOP: Auf Objekt einer Klasse von einer anderen Klasse aus zugreifen (3 answers)
Opened by trequ at 2008-12-26 12:37

Gast Gast
 2008-12-26 13:25
#117439 #117439
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
use strict;
use warnings;
use Logfile;
use Communication;

my $log = new Logfile (...);
my $com = new Communication (...);

$log->open(...);
$log->write(...) if($log->is_open());
$com->do_something();
$log->write($com->get_status()) if($log->is_open());
$log->close(...);


Code (perl): (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
use strict;
use warnings;
use Logfile;
use Communication;

my $log = new Logfile (...);
my $com = new Communication ($log, ...);
$log->open(...);
$log->write(...) if($log->is_open());

$com->do_something();

$log->close(...);

....
package Communication;

sub new
{
  my $class=shift;
  my $log=shift;
  ....
  $self->{LOG}=$log;
  ....
  bless($self, $class);
  return $self;
}

sub do_something
{
 my $self=shift;
  ....
  my $log=$self->{LOG};
  $log->write(...) if($log && $log->is_open());
  ....
}

....


Code (perl): (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
use Logfile;
use Communication;

$log = new Logfile (...);
$log->open(...);

my $func;
$func=sub{
  $data=shift;
  $log->write($data) if($log->is_open());
}

$com = new Communication ($func, ...);

$log->open(...);
$log->write(...) if($log->is_open());

$com->do_something();

$log->close(...);

....
package Communication;

sub new
{
  my $class=shift;
  my $log=shift;
  $log=sub{} if(!($log and ref($log) eq 'CODE'));
  ....
  $self->{LOG}=$log;
  ....
  bless($self, $class);
  return $self;
}

sub do_something
{
 my $self=shift;
  ....
  $self->{LOG}->(...);
  ....
}
....

View full thread OOP: Auf Objekt einer Klasse von einer anderen Klasse aus zugreifen