2

我有一个脚本,可以解析日志并查找错误和警告。
我想对这个日志使用用户友好的解释。
出于这个原因,我使用notepad.
这是代码:

use v5.16;
use strict;
use warnings;

use Win32::Clipboard;
use threads;
use utf8;

my $kp = Win32::Clipboard->new();

my $output = shift || "out_log.txt";

#my ($input, $output)=@ARGV;
#open my $ih, "<", $input or die "can't open input file with log\n";

open my $oh, ">", $output or die "can't open output file with log\n";
my @alls=split /\n/,$kp->Get();

for my $k(0..$#alls){
    $_ = $alls[$k];
    if(/^ERR.+|^WARN.+/){
        print {$oh} qq(at position $k --> ).$_."\n";
        }
    }
my $thread = 
threads->create(sub{
                $SIG{INT}=sub{die"All good\n";};
                qx(notepad $output);
            }
        );

print qq(type 'y' for quit);
do{
    print "want to quit?\n>" ;
    chomp;
    do{
        say "I will kill this thread";
        $thread->kill('INT') if defined($thread);       
        say "and delete output";
        unlink $output;
        exit(0);
        }if (m/y/);
    }while(<>);

当我试图杀死运行记事本的线程时,它掉下来了。
如何做到这一点,使用信号和线程?可能吗?
以及您对解决方案的想法,请。
谢谢!

4

1 回答 1

1

This isn't working because your SIGINT never gets passed to notepad. So it never gets closed. (And that handler - probably never gets processed).

You need to approach this differently. Look at Win32::Process for some examples of how to spawn/kill a notepad process.

my $ProcessObj;
    Win32::Process::Create( $ProcessObj,
        "C:\\Windows\\system32\\notepad.exe",
        "notepad", 0, NORMAL_PRIORITY_CLASS, "." )
        or die $!;

And then you can use

$ProcessObj -> Kill(1); 

I'd suggest using Thread::Semaphore or some sort of shared variable to decide if you want to kill your notepad.

于 2015-02-07T23:36:52.467 回答