我有以下脚本:
use Modern::Perl;
use List::Compare;
use Set::IntSpan;
use List::Util qw/first/;
use List::MoreUtils qw/firstidx onlyidx/;
use Data::Dumper;
sub get_index_by_data {
my ( $data, $arr ) = @_;
return onlyidx { $_ eq $data } @$arr;
}
sub detect_busy_intervals {
my %params = @_;
$params{epoch_key} = 'epoch' if ( !defined $params{epoch_key} ) ;
my @all_epochs = @ { $params{all_epochs} };
my @free_epochs = map { $_->{ $params{epoch_key} } } @{ $params{data} };
my $lc = List::Compare->new( $params{all_epochs}, \@free_epochs );
my @busy_epochs = $lc->get_Lonly;
@all_epochs = sort { $a <=> $b } @all_epochs;
@free_epochs = sort { $a <=> $b } @free_epochs;
@busy_epochs = sort { $a <=> $b } @busy_epochs;
my @busy_indexes_list = map { get_index_by_data( $_, \@all_epochs) } @busy_epochs;
my $int_span = Set::IntSpan->new(join ",", @busy_indexes_list);
my @spans = spans $int_span;
my @res = ();
for my $i ( @spans ) {
my $busy_start_idx = $i->[0];
my $busy_finish_idx = $i->[1];
my $busy_start_time = $all_epochs[ $busy_start_idx ];
my $busy_finish_time = $all_epochs[ $busy_finish_idx ];
my $prev_free_time_idx = $busy_start_idx - 1;
my $next_free_time_idx = $busy_finish_idx + 1;
my $route = {};
$route->{start} = first { $_->{ $params{epoch_key} } == $all_epochs[$prev_free_time_idx] } @{ $params{data} } ;
$route->{finish} = first { $_->{ $params{epoch_key} } == $all_epochs[$next_free_time_idx] } @{ $params{data} } ;
$route->{start}{epoch} = $params{all_epochs}->[ $busy_start_idx ];
$route->{finish}{epoch} = $params{all_epochs}->[ $busy_finish_idx ];
push @res, $route;
}
return \@res;
}
my @checks_arr = ( 100, 200, 300, 400, 500 );
my $data = [
{ 'epoch' => 100, 'cron_data_id' => 1 },
{ 'epoch' => 500, 'cron_data_id' => 5 },
];
print "Data 1: ".Dumper $data;
my $res = [
{ 'start' => { 'epoch' => 200, 'cron_data_id' => 1 }, 'finish' => { 'epoch' => 400, 'cron_data_id' => 5 } },
];
my $a = detect_busy_intervals( data => $data, all_epochs => \@checks_arr );
print "Result: ".Dumper $a;
print "Data 2: ".Dumper $data;
在函数$data
detect_busy_intervals
中使用变量$data
后,其值更改了(特别是epoch
的值不同)。但是在$params{data}
子目录中没有任何对detect_busy_intervals
的写操作!
任何想法可能有什么问题吗?
我通过perlcritic检查了代码,也许我做了一些语法错误,导致变量更改,但是没有发现问题。
答案 0 :(得分:6)
在这些行中:
$route->{start} = first { $_->{ $params{epoch_key} } == $all_epochs[$prev_free_time_idx] } @{ $params{data} } ;
$route->{finish} = first { $_->{ $params{epoch_key} } == $all_epochs[$next_free_time_idx] } @{ $params{data} } ;
您可以将data
参数中的哈希引用分配到$route
结构中。这些是浅表副本,因此它们引用与data
参数引用的哈希相同的哈希。这意味着这些行:
$route->{start}{epoch} = $params{all_epochs}->[ $busy_start_idx ];
$route->{finish}{epoch} = $params{all_epochs}->[ $busy_finish_idx ];
修改原始哈希。
您可以通过取消引用哈希引用(data
),然后创建一个包含结果列表({{1的新哈希引用),而从%{}
参数分配副本(至少一个副本级别) }}):
{}
my $start = first { $_->{ $params{epoch_key} } == $all_epochs[$prev_free_time_idx] } @{ $params{data} } ;
my $finish = first { $_->{ $params{epoch_key} } == $all_epochs[$next_free_time_idx] } @{ $params{data} } ;
$route->{start} = { %{ $start // {} } };
$route->{finish} = { %{ $finish // {} } };
确保即使您对// {}
的调用返回undef(当找不到匹配项时),取消引用也只会导致一个空列表。
如果您的数据结构中可能还包含嵌套的引用,那么通常的解决方案是从listed here之类的模块中复制内容。
first