#! /usr/bin/env perl
use strict;
use warnings;
my $prev_return;
while (<>) {
# assume letter in first column introduces a function return type
if (my($return_type) = /^([^\W\d]\w*)/) {
if ($return_type eq "void" && defined $prev_return) {
print $prev_return;
}
undef $prev_return; # reset after each function definition
}
elsif (/\\return\b/) {
$prev_return = "$ARGV:$.: $_";
}
}
continue {
close ARGV if eof; # reset $. at the end of each named file
}
注:該模式[^\W\d]
用途double-negative technique匹配不是數字的「單詞字符」,即,字母和下劃線。
輸出示例:
$ cat file.c
/*!
* \brief The foo function
* \return OK if successful, ERROR otherwise
*/
void foo(void)
{
...
}
\return fdsklj
void bar(void)
void baz
$ ./doxygen-return-void file.c
file.c:3: * \return OK if successful, ERROR otherwise
file.c:10: \return fdsklj
Perl的<>
,又稱鑽石操作,使得編寫Unix風格的過濾器容易。這意味着您可以根據需要命名儘可能多的路徑。
$ ./doxygen-return-void file.c file.c
file.c:3: * \return OK if successful, ERROR otherwise
file.c:10: \return fdsklj
file.c:3: * \return OK if successful, ERROR otherwise
file.c:10: \return fdsklj
上面的程序也將消耗標準輸入,但輸出不是非常有用。
$ cat file.c | ./doxygen-return-void
-:3: * \return OK if successful, ERROR otherwise
-:10: \return fdsklj
可能downvoter照顧詳細說明我的問題出了什麼問題? – Jens