hi
Gibt es eine Elegantere Möglichkeit für eine vererbte Methode
ein Attribut zu Setzen und zu verwenden ?
siehe Beispiel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/perl
use strict qw(vars);
use warnings;
use English '-no_match_vars';
use Carp;
no warnings 'experimental::smartmatch';
use feature 'switch';
use lib 'lib/';
use OOTest::Out qw(out);
# vererbt
out ("hello world\n");
OOTest::Out::setopt ( 'debug', '1' );
out ("hello world\n");
# als objekt
my $o = OOTest::Out->new();
$o->out2 ("hello world\n");
$o->{'debug'} = '1' ;
$o->out2 ("hello world\n");
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package OOTest::Out;
use warnings;
use feature qw(switch);
use experimental 'switch';
use English '-no_match_vars';
use strict;
use Carp;
require Exporter;
use Exporter qw(import);
use base qw(Exporter);
our @EXPORT_OK = qw(out);
use vars qw($VERSION);
use Data::Dumper;
$VERSION = '0.01';
sub new {
my ($class,$args) = @_;
my $self = {
debug => '0',
};
bless $self, $class;
return $self;
}
my $o = OOTest::Out->new();
sub out {
my $txt = shift ;
if ( $o->{'debug'} eq '1' ) {
print {*STDOUT} "[DEBUG][out]$txt\n" or carp $ERRNO;
} else {
print {*STDOUT} "[out]$txt\n" or carp $ERRNO;
}
return ;
}
sub out2 {
my ( $self , $txt ) = @_;
if ( $self->{'debug'} eq '1' ) {
print {*STDOUT} "[DEBUG][out2]$txt\n" or carp $ERRNO;
} else {
print {*STDOUT} "[out2]$txt\n" or carp $ERRNO;
}
return ;
}
sub setopt {
my ( $arg,$val ) = @_;
$o->{ $arg } = $val ;
return ;
}
1;
Der Teil mit der Überschrift "vererbt" ist das was ich will ,
jedoch stell ich it die frage ob das eleganter geht.
Holger