複数行の文字列を一度に 1 行ずつ処理するための適切な PBP 承認済みの方法を見つけようとしています。多くの Perl コーダーは、複数行の文字列をファイル ハンドルとして扱うことを提案しています。次に、厳密な参照が使用されている間は文字列をシンボルとして使用できないという警告がコンパイラから表示されます。
問題の簡単な実例を次に示します。
#use strict;
use warnings;
my $return = `dir`;
my $ResultsHandle = "";
my $matchLines = "";
my $resultLine = "";
open $ResultsHandle, '<', \$return;
while (defined ($resultLine = <$ResultsHandle>)) {
if ($resultLine =~ m/joe/) {
$matchLines = $matchLines . "\t" . $resultLine;
}
}
close($ResultsHandle);
print "Original string: \n$return\n";
print "Found these matching lines: \n$matchLines\n";
「use strict」行がコメントアウトされていることに注意してください。このスクリプトを厳密に使用せずに実行すると、必要なものと期待するものが得られます。
Original string:
Volume in drive D has no label.
Volume Serial Number is 50D3-54A6
Directory of D:\Documents and Settings\username\My Documents\Eclipse\myTestProject
09/18/2009 11:38 AM <DIR> .
09/18/2009 11:38 AM <DIR> ..
09/18/2009 11:36 AM 394 .project
09/18/2009 11:37 AM 0 joe.txt
09/18/2009 11:37 AM 0 joey.txt
09/18/2009 11:38 AM 0 kurt.txt
09/18/2009 11:43 AM 497 main.pl
09/18/2009 11:38 AM 0 shane.txt
6 File(s) 891 bytes
2 Dir(s) 6,656,188,416 bytes free
Found these matching lines:
09/18/2009 11:37 AM 0 joe.txt
09/18/2009 11:37 AM 0 joey.txt
ただし、ここに問題があります。「use strict」行のコメントを外すと、Perl から次の警告またはエラーが表示されます。
Can't use string ("") as a symbol ref while "strict refs" in use at D:/Documents and Settings/username/My Documents/Eclipse/myTestProject/main.pl line 8.
8 行目は「open $ResultsHandle, '<', \$return;」です。ちなみにライン。Perl のベスト プラクティスでは strict を使用する必要があるため、複数行の文字列を一度に 1 行ずつ処理することを PBP はどのように期待していますか? SO コミュニティからの提案はありますか?
ありがとう!