--- /dev/null
+use strict;
+use warnings FATAL => 'all';
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ NAME => 'lib-with-preamble',
+ VERSION => 0,
+ PM_FILTER => 'perl my/filter',
+ PREREQ_PM => { 'PerlIO::via::dynamic' => 0.02 },
+);
--- /dev/null
+package lib::with::preamble;
+
+use strict;
+use warnings FATAL => 'all';
+use File::Spec;
+use PerlIO::via::dynamic;
+
+sub require_with_preamble {
+ my ($arrayref, $filename) = @_;
+ my (undef, $preamble, @libs) = @$arrayref;
+ foreach my $cand (map File::Spec->catfile($_, $filename), @libs) {
+ if (-f $cand) {
+ if (open my $fh, '<', $cand) {
+ return with_preamble($preamble."\n#line 1 $cand\n", $fh);
+ }
+ }
+ }
+}
+
+sub with_preamble {
+ my ($preamble, $fh) = @_;
+ PerlIO::via::dynamic->new(untranslate => sub {
+ $preamble and $_[1] =~ s/\A/$preamble/, undef($preamble);
+ })->via($fh);
+ return $fh;
+}
+
+sub import {
+ my ($class, $preamble, @libs) = @_;
+ return unless defined($preamble) and @libs;
+ unshift @INC, [ \&require_with_preamble, $preamble, @libs ];
+}
+
+1;
--- /dev/null
+package lib::with::preamble::example::strict;
+
+$orz++; # should die
+
+1;
--- /dev/null
+use strict;
+use warnings FATAL => 'all';
+use Test::More qw(no_plan);
+use lib::with::preamble 'use v5.10;', 't/lib';
+
+ok(eval { require my_given_example; 1 }, 'Loaded module');
+
+sub result_for { eval { my_given_example::example_sub($_[0]) } }
+
+is(result_for(1), 'positive');
+is(result_for(-1), 'negative');
+is(result_for(0), 'zero');
+
+is(my_given_example::my_file(), 't/lib/my_given_example.pm');
+is(my_given_example::my_line(), 12);
--- /dev/null
+package my_given_example;
+
+sub example_sub {
+ given ($_[0]) {
+ when ($_ > 0) { return 'positive' }
+ when ($_ < 0) { return 'negative' }
+ return 'zero'
+ }
+}
+
+sub my_file { __FILE__ }
+sub my_line { __LINE__ }
+
+1;
--- /dev/null
+use strict;
+use warnings FATAL => 'all';
+use Test::More qw(no_plan);
+
+ok(
+ !eval { require lib::with::preamble::example::strict; 1 },
+ 'strict example dies'
+);
+
+like($@, qr{Global symbol "\$orz" requires explicit package name at \S+lib/with/preamble/example/strict.pm line 3}, 'Error has right name and line');