ingydotnet / test-base-pm

Extendable Perl Testing
http://search.cpan.org/dist/Test-Base/
13 stars 12 forks source link

Reading from *main::DATA can be wrong #22

Open perlpunk opened 5 years ago

perlpunk commented 5 years ago

See https://github.com/ingydotnet/test-yaml-pm/issues/3 and https://github.com/ingydotnet/yaml-pm/issues/210

% echo "Test::YAML 0" | perl .../lib/perl5/CPAN/Reporter/PrereqCheck.pm
Test::YAML 1 1.06
Can't determine section names at .../lib/perl5/CPAN/Reporter/PrereqCheck.pm line 0.
END failed--call queue aborted, <DATA> line 1.
1..0

I tried to fix this with https://github.com/ingydotnet/test-base-pm/pull/21 but the fix only works if the main::DATA section is empty.

Not sure how to solve this.

mohawk2 commented 5 years ago

The cause of this is what's in http://perldoc.perl.org/perldata.html, about __END__:

For compatibility with older scripts written before __DATA__ was introduced, __END__ behaves like __DATA__ in the top level script (but not in files loaded with require or do) and leaves the remaining contents of the file accessible via main::DATA.

Repro to demonstrate:

$ cat >>tf2.pl <<EOF; perl tf2.pl
package Other; require Scalar::Util; print Scalar::Util::openhandle( \*main::DATA ), "\n";
__END__
Yo
EOF

It prints a GLOB(blah) with and without the package declaration.

mohawk2 commented 5 years ago

Context: this happens with:

$ echo "Test::YAML 0" | perl `perldoc -l CPAN::Reporter::PrereqCheck`

CPAN::Reporter::PrereqCheck has an __END__ section. If called as a script (rather than loaded with -M etc), it behaves in the above-mentioned way.

Since CPAN::Reporter::PrereqCheck has a number of exceptions of modules to not load, it seems reasonable to simply add Test::YAML to that list of exceptions.