今日の発見 From Test::Base

Test::Baseのスペシャル機能で、use strict; use warnings; を自動的に追加するってあった。
試してみた。

Test::Baseなし

#!/opt/local/bin/perl

use Perl6::Say;
my $a = hi_all;
say $a ;

結果

hi_all

Hit ENTER or type command to continue

Test::Base付き

#!/opt/local/bin/perl

use Test::Base;
use Perl6::Say;
my $a = hi_all;
say $a ;

結果

Bareword "hi_all" not allowed while "strict subs" in use at n.pl line 5.
Execution of n.pl aborted due to compilation errors.

shell returned 255

Hit ENTER or type command to continue

ワーィ。これからはすべてのスクリプトに、Test::Baseだな。(嘘)

追記

Test::Baseのコードをパクって実装してみた。

package AlwaysUseMe;
use Filter::Util::Call ;

sub import {
    my $done = 0;
    filter_add(
            sub {
                return 0 if $done;
                my ($data, $end) = ('', '');
                while (my $status = Filter::Util::Call::filter_read()) {
                    return $status if $status < 0;
                    if (/^__(?:END|DATA)__\r?$/) {
                    $end = $_;
                    last;
                    }
                    $data .= $_;
                    $_ = '';
                }
                $_ = "use strict;use warnings;$data$end";
                $done = 1;
                }
            );
}

1
#!/opt/local/bin/perl

use AlwaysUseMe;
use Perl6::Say;

my $a = a;
say $a;

結果

Bareword "a" not allowed while "strict subs" in use at a.pl line 6.
Execution of a.pl aborted due to compilation errors.

shell returned 255

Hit ENTER or type command to continue

さらに追記

Filter::Simpleを使って実装してみた

package AlwaysUseMe2;
use Filter::Simple;

FILTER {  $_ = 'use strict; use warnings;' . $_ ; }

1;
#!/opt/local/bin/perl

use AlwaysUseMe2;
use Perl6::Say;

my $b = b;

say $b;

結果

Bareword "b" not allowed while "strict subs" in use at b.pl line 6.
Execution of b.pl aborted due to compilation errors.

shell returned 255

Hit ENTER or type command to continue

ワーィ