简体   繁体   English

Perl替代:无法使用变量perl regex替代

[英]Perl Substitute: Unable to substitute using variables perl regex

The following code reads defines.c file line-by-line and stores all the "#define" directives in a hash (with defines name and its replacement text). 以下代码逐行读取define.c文件,并将所有"#define"指令存储在哈希中(带有定义名称及其替换文本)。 Then it replaces them if the usage is find on the consecutive lines. 然后,如果在连续的行中找到用法,它将替换它们。

I have an issue with the substitution if a "#define" directive is being used. 如果正在使用"#define"指令,替换会有问题。 It is storing all the #defines properly in the hash. 它将所有#defines正确存储在哈希中。

I am a beginner in perl and regex and unable to point out the silly thing as to why it's not working. 我是perl和regex的初学者,无法指出为什么它不起作用这一愚蠢的事情。
Any help? 有什么帮助吗?

#!/usr/bin/perl -w

use strict;
my %definesHash;

open(FILE, "defines.c") || die "Cannot open $!\n";
open(OUT, ">defines.i") || die "Cannot open $!\n";


while (<FILE>)
{
 my $line = $_;
 if ($line =~ /#define\s+/)
 {
   $line =~ s/#define\s+//g;
   if ($line =~ /\b([\w]+)\b\s+/)
   {
    my $define = $1;
    $line =~ s/\b[\w]+\b\s+//;
    $definesHash{$define} = "";
    if($line =~ /\s*(.*)\s*/)
    {
      $definesHash{$define} = $1;
    }
   }
   print OUT $_;
 }
 else
 { 
   my($def, $replace);
   while (($def, $replace) = each(%definesHash))
   {
     print " $def => $replace \n";
     if ($line =~ /$def/)
     {
      $line =~ s/$def/$replace/g; #****** Some Problem Here, But What? ********
     }
   }
   print OUT $line;
 }
}

close(FILE);
close(OUT);

defines.c defines.c

#include <stdio.h>

#define VALUE 10
#define PLACE (20 + 0)
#define NUM (VALUE + 10)
int main()
{
  int num;
  num = NUM + 25 + NUM + PLACE;
  return 0;
}

Expected Output: **defines.i**

#include <stdio.h>

#define VALUE 10
#define PLACE (20 + 0)
#define NUM (VALUE + 10)
int main()
{
  int num;
  num = (10 + 10) + 25 + (10 + 10) + (20 + 0);
  // or
  num = (VALUE + 10) + 25 + (VALUE + 10) + (20 + 0);
  return 0;
}  

I know the problem is in the substitution as I have pointed in the comment. 正如我在评论中指出的那样,我知道问题出在替代上。 I can see the original line becoming gibberish after that substitution whereas the contents in the hash seem to be correct. 我可以看到替换后原始行变得乱七八糟,而哈希中的内容似乎是正确的。
Is it with the usage of variables in substitution? 是否使用替代变量?

It looks like your script depends on the order (or rather, disorder) of the hash. 看来您的脚本取决于哈希的顺序(或更确切地说,是无序)。

#define VALUE 10
#define NUM (VALUE + 10)

The definition doesn't get updated because that replacement takes place in the else -- so replacement only happens on lines without #define . 该定义不会更新,因为该替换发生在else -因此,替换仅发生在没有#define

However, you perform the replacements based on whatever order each gives you - so it could be trying to replace VALUE , and then trying to replace NUM . 但是,您将根据each给您的订单执行替换-因此,它可能会尝试替换VALUE然后尝试替换NUM

I'd probably perform the initial replacements in each definition as they come, something like this: 我可能会在每个定义出现时执行初始替换,如下所示:

#!/usr/bin/perl

use warnings;
use strict;
my %definesHash;

open(my $fh_in,  '<', "defines.c") || die "Cannot open $!\n";
open(my $fh_out, '>', "defines.i") || die "Cannot open $!\n";

while (<$fh_in>) {
    if (/^#define\s+(\w+)\s+(.*)\s*$/) {
        my ($define, $replacement) = ($1, $2);
        # perform existing replacements on
        # the current $replacement
        while (my ($def, $replace) = each %definesHash) {
            $replacement =~ s/$def/\Q$replace/g;
        }
        $definesHash{$define} = $replacement;
    }
    else {
        while (my ($def, $replace) = each(%definesHash)) {
            print " $def => $replace \n";
            s/$def/$replace/g;
        }
    }

    print $fh_out $_;
}

close($fh_in);
close($fh_out);

Or it could be the unescaped regex metacharacters, here escaped by \\Q . 或者它可能是未转义的正则表达式元字符,在此由\\Q转义。

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

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