gpt4 book ai didi

perl - 使用子程序时出现错误 "variable will not stay shared"

转载 作者:行者123 更新时间:2023-12-04 01:30:13 28 4
gpt4 key购买 nike

我写了一个函数,这里是:

use strict;
use warnings;
use feature 'say';
use JSON;
use utf8;

sub process {
my %IDs = ( "User awx01 logged in." => 1001 );
my %levels = ( INFO => 4 );
my $data = do { local $/; <DATA> };

# read in all the data, even though it looks

my $decoded = decode_json( $data );
$decoded->{Message} = decode_json( $decoded->{Message} );

say rec2msg($decoded);

sub rec2msg {
my $r = shift;
$r->{Message}{message} =~ /(\w+) (\w+) (.+)/;

my($user,$msg) = ($2,"$1 $3");
my $ID = $IDs{$r->{Message}{message}};
my $level = $levels{$r->{Message}{level}};

my $out = "$r->{Message}{'@timestamp'} host CEF:0|OpenSource|AWX|7.0.0|$ID|$msg|$level|src=127.0.0.1 dst=$r->{MessageSourceAddress} duser=$user";
return $out;
}

}
__DATA__
{"MessageSourceAddress":"192.168.81.20","EventReceivedTime":"2020-02-06 11:55:14","SourceModuleName":"udp","SourceModuleType":"im_udp","SyslogFacilityValue":1,"SyslogFacility":"USER","SyslogSeverityValue":5,"SyslogSeverity":"NOTICE","SeverityValue":2,"Severity":"INFO","EventTime":"2020-02-06 11:55:14","Hostname":"192.168.81.20","Message":"{\"@timestamp\": \"2020-02-06T08:55:52.907Z\", \"message\": \"User awx01 logged in.\", \"host\": \"awxweb\", \"level\": \"INFO\", \"logger_name\": \"awx.api.generics\", \"stack_info\": null, \"type\": \"other\", \"cluster_host_id\": \"awx-contr-01\", \"tower_uuid\": \"333b4131-495f-4460-8e4b-890241a9d73d\"}"}

运行此代码后所需的输出是:
2021-02-06T08:55:52.907Z host CEF:0|OpenSource|AWX|7.0.0|1001|User logged in.|4|src=127.0.0.1 dst=192.168.81.20 duser=awx01

但是,当我运行此代码时,出现此错误:

变量 "%IDs"不会在/usr/libexec/nxlog/modules/extension/perl/event1.pl 第 25 行保持共享。
变量 "%levels"不会在/usr/libexec/nxlog/modules/extension/perl/event1.pl 第 26 行保持共享。

我怎么能修好呢?我真的需要它在一个功能中

我试过这个,但没有用:
use strict;
use warnings;
use feature 'say';
use JSON;
use utf8;

sub process {
my %IDs = ( "User awx01 logged in." => 1001 );
my %levels = ( INFO => 4 );
my $data = do { local $/; <DATA> };

# read in all the data, even though it looks

my $decoded = decode_json( $data );
$decoded->{Message} = decode_json( $decoded->{Message} );

say rec2msg($decoded);

local *rec2msg = sub {
my $r = shift;
$r->{Message}{message} =~ /(\w+) (\w+) (.+)/;

my($user,$msg) = ($2,"$1 $3");
my $ID = $IDs{$r->{Message}{message}};
my $level = $levels{$r->{Message}{level}};

my $out = "$r->{Message}{'@timestamp'} host CEF:0|OpenSource|AWX|7.0.0|$ID|$msg|$level|src=127.0.0.1 dst=$r->{MessageSourceAddress} duser=$user";
return $out;
}
return rec2msg();


}
__DATA__
{"MessageSourceAddress":"192.168.81.20","EventReceivedTime":"2020-02-06 11:55:14","SourceModuleName":"udp","SourceModuleType":"im_udp","SyslogFacilityValue":1,"SyslogFacility":"USER","SyslogSeverityValue":5,"SyslogSeverity":"NOTICE","SeverityValue":2,"Severity":"INFO","EventTime":"2020-02-06 11:55:14","Hostname":"192.168.81.20","Message":"{\"@timestamp\": \"2020-02-06T08:55:52.907Z\", \"message\": \"User awx01 logged in.\", \"host\": \"awxweb\", \"level\": \"INFO\", \"logger_name\": \"awx.api.generics\", \"stack_info\": null, \"type\": \"other\", \"cluster_host_id\": \"awx-contr-01\", \"tower_uuid\": \"333b4131-495f-4460-8e4b-890241a9d73d\"}"}

最佳答案

使用嵌套的命名子例程有点不寻常。
该错误消息的 The documentation 提供了一些建议:

Variable "%s" will not stay shared

(W closure) An inner (nested) named subroutine is referencing a lexical variable defined in an outer named subroutine.

When the inner subroutine is called, it will see the value of the outer subroutine's variable as it was before and during the first call to the outer subroutine; in this case, after the first call to the outer subroutine is complete, the inner and outer subroutines will no longer share a common value for the variable. In other words, the variable will no longer be shared.

This problem can usually be solved by making the inner subroutine anonymous, using the sub {} syntax. When inner anonymous subs that reference variables in outer subroutines are created, they are automatically rebound to the current values of such variables.


所以你可以遵循这个建议,或者完全摆脱内部子程序。一眼看去,我看不出任何真正的原因。

关于perl - 使用子程序时出现错误 "variable will not stay shared",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61186639/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com