Thread code innerhalb von {} (kA ^^) (6 answers)
Opened by keksinat0r at 2008-02-05 20:00

murphy
 2008-02-05 20:22
#105600 #105600
User since
2004-07-19
1776 Artikel
HausmeisterIn
[Homepage]
user image
Um das gewünschte Verhalten von ErrorHandler zu erreichen empfiehlt sich die Verwendung des & Prototypen. Irgendwie so sollte es gehen:
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
sub ErrorHandler(&) {
  my $block = shift;

  my $output = '';
  my $warnings = '';

  {
    local *STDOUT;
    tie *STDOUT, 'Some::Class::Collecting::The::Output', \$output;

    local $SIG{__WARN__} = sub {
      local $_ = shift;
      $warnings .= $_;
      $warnings .= "\n" unless (/\n$/);
    };

    eval {
      $block->();
    }
  }

  if ($@) {
    # handle the errors
  }
  elsif ($warnings) {
    # handle the warnings
  }
  else {
    print $output;
  }
}

When C++ is your hammer, every problem looks like your thumb.

View full thread code innerhalb von {} (kA ^^)