Pod::Usageに依存して呼び出しスクリプトの POD を解析し、使用法、ヘルプ、およびマニュアル テキストをスカラー変数に送信するモジュールに取り組んでいます。そのテキストから最後の空白を削除する必要があったため、機能すると思われる単純な正規表現を使用しました。そしてそれは...しかし断続的に。
これが問題のデモンストレーションです。任意の洞察をいただければ幸いです。
Perl 5.10.1 を搭載した Solaris マシンで、予期しない動作 (つまり、正規表現が最後の改行を削除できない) が一貫して発生します。Windows で Perl 5.12.1 を使用すると、動作が不安定になります (以下に出力を示します)。
use strict;
use warnings;
use Pod::Usage qw(pod2usage);
use Test::More;
# Baseline test to show that the regex works.
my $exp = "foo\nbar\n...";
my $with_trailing_whitespace = $exp . " \n\n";
$with_trailing_whitespace =~ s!\s+\Z!!;
my $ords = get_ords_of_final_chars($with_trailing_whitespace);
is_deeply $ords, [46, 46, 46]; # String ends with 3 periods (not whitespace).
# Run a similar test, using text from Pod::Usage.
for (1 .. 2){
my $pod = get_pod_text();
$ords = get_ords_of_final_chars($pod);
is_deeply $ords, [46, 46, 46];
}
done_testing();
sub get_ords_of_final_chars {
# Takes a string. Return array ref of the ord() of last 3 characters.
my $s = shift;
return [ map ord(substr $s, - $_, 1), 1 .. 3 ];
}
sub get_pod_text {
# Call pod2usage(), sending output to a scalar.
open(my $fh, '>', \my $txt) or die $!;
pod2usage(-verbose => 2, -exitval => 'NOEXIT', -output => $fh);
close $fh; # This doesn't help.
# Here's the same regex as above.
#
# If I use chomp(), the newlines are consistently removed:
# 1 while chomp($txt);
$txt =~ s!\s+\Z!!;
return $txt;
}
__END__
=head1 NAME
sample - Some script...
=head1 SYNOPSIS
foo.pl ARGS...
=head1 DESCRIPTION
This program will read the given input file(s) and do something
useful with the contents thereof...
=cut
私のWindowsボックスでの出力:
$ perl demo.pl
ok 1
not ok 2
# Failed test at demo.pl line 18.
# Structures begin differing at:
# $got->[0] = '10'
# $expected->[0] = '46'
not ok 3
# Failed test at demo.pl line 18.
# Structures begin differing at:
# $got->[0] = '10'
# $expected->[0] = '46'
1..3
# Looks like you failed 2 tests of 3.
$ perl demo.pl
ok 1
ok 2
ok 3
1..3