Since the OP's
sub l is within the same
BEGIN block as
use bytes this isn't really the issue (although placing the
use bytes alone in the begin block causes the pragma to be scoped only to the
BEGIN block. I remembered seeing an example of this type of thing in the source of
RPC::XML. I'm guessing there's no simple way around the problem but here's one option (which you might have already discovered based on your original post):
package Y;
$VERSION = 0.1;
1;
package X;
my $subs =<<__SUBS__
sub l{ return length shift };
__SUBS__
if ( $Y::VERSION < 1 ){
use bytes;
eval $subs;
} else {
eval $subs;
}
+
+
# large module follows
+
+
1;
package main;
no bytes;
$x = chr(3456);
print X::l($x);
BTW,
use MODULE is the different than
use PRAGMA. You can't use
BEGIN { require PRAGMA import PRAGMA LIST; } when you're dealing with pragmas. The
BEGIN {} creates a lexical scope of it's own and defeats the purpose of pragmas that affect the lexical scope. Plus, you wouldn't ever be able to do an
import on the pragma.
Update: Solutions suggested below using the
if module are much more reasonable than what I posted... Oh well :-)