Quelle:
http://perl-6.de/
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
use v6;
my $fn = $*PROGRAM_NAME;
my $file = open($fn) err die "Can't read file '$fn': $!";
my %braces = ( '(' => ')',
'{' => '}',
'[' => ']',
);
my @stack;
for =$file -> my $line {
for ($line.comb(m/<[()\[\]\{\}]>/)) -> my $match {
if %braces{$match} {
# öffnende Klammer gefunden
push @stack, ~$match;
} else {
# schliessende Klammer gefunden
my $expected = %braces{pop @stack};
if $expected ne $match {
die "File '$fn' is not balanced: expected '" ~
$expected ~ "', found '$match'\n";
}
}
}
}
$file.close;
if @stack.elems == 0 {
say "File $fn is balanced";
} else {
say "File $fn is not balanced, more opening braces than closing ones";
}
da muss man ja ganz schön umdenken!\n\n
<!--EDIT|Froschpopo|1179579404-->