perl - 从函数定义中提取参数并将其作为注释放在它上面 [英] perl - extracting arguments from function definitions and putting it as comment above it

查看:69
本文介绍了perl - 从函数定义中提取参数并将其作为注释放在它上面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ............
 ########### NEED TO PUT ARGUMENTS HERE AS COMMENT #########
 eErrorT ChainCtrlInitChains(ChainCtrlT* pChainCtrl,
    char* name,
    int instance,
    void* pOwner,
    )
    {
       ....
    }
     .........

我想提取并把它作为注释放在函数定义之上.类似的函数定义有很多.

i want to extract and put it above function definition as comment. There are many similar function definitions.

open(my $FILE1, "< a.c") or die $!;
@arr = <$FILE1>;

foreach(@arr){
    if($_ =~ /^ \S+ \s+ \S+ \s* \( (.+?) \) /xsmg) {               
      my $arg = $1;
      my @arr = map /(\w+)$/, split /\W*?,\W*/, $arg;
      print @temp = map ' *  @param[in/out] '."$_\n", @arr
          unless $_ =~ /;\s*$/;
     }
}

它在我使用 $str 时有效,但是我无法将参数拼接为函数定义上方的注释.

It works when I use $str, But then I can't splice the arguments as comment above the function definitions.

$str = <$FILE1>;    

推荐答案

给你:

use File::Copy;
open my $FILE,'<','a.c' or die "open failed: $!\n";
$file_slurp = do { local $/;<$FILE>};
$file_slurp =~ s{ ^ ( \w+ \s+ \w+ \s* \( (.+?) \) )}{&print_args($2,$1)}xmesg;
close($FILE) or die "Couldn't close file: $!\n";
copy "a.c","a.c.bak" or die "Copy failed: $!\n";
open my $NEW_FILE,'>','a.c' or die "Truncating a.c failed: $!\n";
print $NEW_FILE $file_slurp and unlink "a.c.bak";

sub print_args {
    ($args,$proto) = @_;
    @comments = map { ' * @param[in/out] '."$_" } split /\s*(?:^|,)\s*/,$args;
    return join "\n",(@comments,$proto)
}

首先通过删除 unlink 来测试代码,以便将源的备份副本保留在磁盘上.当您确信它可以满足您的要求时,您可以恢复取消链接,这样您的原始文件似乎已被修改到位.

Test the code first by removing the unlink so that a backup copy of your source is kept on disk. When your'e confident it does what you want, you can put back the unlink so that it seems that your original file was modified in place.

这篇关于perl - 从函数定义中提取参数并将其作为注释放在它上面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆