简体   繁体   English

在 Perl 中取消引用会产生意想不到的结果

[英]Dereferencing in Perl gives unexpected result

Sub1 can apply a regex1 on an input string (if it's defined) and collect the references to the special hash ( %+ ) and arrays ( @- and @+ ) variables for captured groups in an objs array. Sub1 可以在输入字符串(如果已定义)上应用 regex1,并收集对 objs 数组中捕获组的特殊 hash ( %+ ) 和 arrays ( @-@+ ) 变量的引用。 Alternatively, the calling function can directly pass the references to special hash and arrays which are directly added to the objs array.或者,调用 function 可以直接传递对特殊 hash 和 arrays 的引用,它们直接添加到 objs 数组中。 Sub1 then iterates over the information in objs array, (does something) and calls Sub2 to further work on a subset of the captured groups.然后 Sub1 遍历 objs 数组中的信息,(做某事)并调用 Sub2 进一步处理捕获组的子集。

Sub2 can also apply a regex2 on an input string (if it's defined) and collect the references in another local objs array or directly collect the references to the captured groups in the local objs array depending on how it was called. Sub2 还可以在输入字符串(如果已定义)上应用 regex2,并在另一个本地 objs 数组中收集引用,或者根据调用方式直接在本地 objs 数组中收集对捕获组的引用。 It then iterates over the objs array and do something with it - however, when I dereference it, it contains information about matched groups of sub1 and not the matched groups collected in Sub2 as expected.然后它遍历 objs 数组并用它做一些事情 - 但是,当我取消引用它时,它包含有关 sub1 的匹配组的信息,而不是 Sub2 中收集的匹配组的信息,如预期的那样。

sub sub1{
  my ($content, $grp_hash_ref, $grp_st_ref, $grp_end_ref) = @_;
  my @objs = ();
  if (defined $content){
    while($content =~ m/<some_regex1>/g){
      push(@objs, [\%+, \@-, \@+]);
    }
  } else {
    push(@objs, [$grp_hash_ref, $grp_st_ref, $grp_end_ref]);
  }
  for my $row (@objs){
    my @obj = @$row;
    my (%grp_hash, @grp_st, @grp_end);
    %grp_hash = %{ $obj[0] };
    @grp_st   = @{ $obj[1] };
    @grp_end  = @{ $obj[2] };
    #####################
    # Do something here
    #####################
    # call sub2
    my $new_content = $+{'<some_group_name>'};
    ... = sub2($new_content, undef, undef, undef);
  }
}

sub sub2{
  my ($content, $grp_hash_ref, $grp_st_ref, $grp_end_ref) = @_;
  my @objs = ();
  if (defined $content){
    while($content =~ m/<some_regex2>/g){
      push(@objs, [\%+, \@-, \@+]);
    }
  } else {
    push(@objs, [$grp_hash_ref, $grp_st_ref, $grp_end_ref]);
  }
  for my $row (@objs){
    my @obj = @$row;
    my (%grp_hash, @grp_st, @grp_end);
    %grp_hash = %{ $obj[0] };
    @grp_st   = @{ $obj[1] };
    @grp_end  = @{ $obj[2] };
    print(Dumper(\%grp_hash));  # This incorrectly prints capture groups of regex1 used in sub1
    # I expect capture gorup information of regex2

  }
}
while($content =~ m/<some_regex1>/g){
  push(@objs, [\%+, \@-, \@+]);
}

This stores references to the same three variables over and over again.这会一遍又一遍地存储对相同三个变量的引用 Every time you match a regex those variables will change.每次匹配正则表达式时,这些变量都会改变。

You need to instead store copies.您需要存储副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM