Leser: 1
10 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
sub new {
my ($class, %args) = @_;
# ...
my $self = bless {
# ...
# Das soll der Hash sein:
_config => {},
}, $class;
return $self;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
sub new {
my ($class, %args) = @_;
$args{path} = $ENV{HOME} . "/www/" unless defined $args{path};
my $self = bless {
_file => $args{file},
_path => $args{path},
_config => {},
}, $class;
return $self;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
sub open_file {
my ($self) = @_;
# ...
while (<CONFIG>) {
# ... (String-Bearbeitung)
my ($var, $value) = split(/\s*=\s*/, $_, 2);
$self->{_config}->{$var} = $value;
}
# ...
}
1
2
3
4
5
6
7
my $config = Configuration->new(file => "config",
path => "./");
$config->open_file();
my %hash = $config->get_config();
print $hash{server};
my %hash = $config->get_config();
my %hash = %{$config->get_config()};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
my @files = ("testfile", "irgendwas");
for (@files) {
my $lines = count_lines($_);
print "$_ hat $lines Zeilen.\n"; # Achte genau auf die Ausgabe
}
sub count_lines {
my $filename = shift;
open my $F, "<", $filename or die "Kann $filename nicht oeffnen: $!\n";
my $counter=0;
# In einem guten Script wuerde an dieser Stelle local $_; stehen
while (<$F>) {
++$counter;
}
return $counter;
}
1
2
3
4
5
6
7
8
9
10
11
my $config = Configuration->new(file => "config",
path => "./");
$config->open_file();
my %hash = $config->get_config();
# Gibt 'mysql' aus...
print $hash{Server} . "\n";
# Klappt auch wunderbar...
$hash{Server} = "test";
10 Einträge, 1 Seite |