in reply to impose 'use bytes' on another package

Lets try a simpler problem first. An evaluation of the main code of Foo.pm
perl -e 'use bytes; eval q(package Foo; sub len{ length "\x{03c5}"});p +rint Foo::len()' 2
I always thought do "Foo.pm" is the same as eval `cat Foo.pm`. But
$ perl -e 'use bytes; do "Foo.pm";print Foo::len()' 1

Replies are listed 'Best First'.
Re^2: impose 'use bytes' on another package
by codeacrobat (Chaplain) on Apr 05, 2006 at 23:12 UTC
    Oops forget to post the workaround. All you have to do is get rid of the 1; in the Foo.pm
    and eval the remaining content of the (no longer)Module.
    $code = `cat Foo.pm`;$code =~ s/\n1;//s; eval $code;
    Use it if a quick'n dirty solution is right for you. Otherwise I hope other monks come up with a cleaner solution.

      You don't have to remove trailing true value and it'd have been nicer if you avoided making a call to the shell and cat when there are perfectly good perl functions for such a thing. This is also a quick and dirty solution but it isn't as craptacular as yours.

      local @ARGV = "Foo.pm"; # TODO: make this search @INC local $/; eval "#line Foo.pm 1\nuse bytes;" . <>; die $@ if $@;

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        I agree your solution works better.
        use Foo; my ($foo) = grep {/Foo\.pm$/} values %INC; { # so that local can be local local @ARGV = $foo; local $/; eval "#line Foo.pm 1\nuse bytes;" . <>; } die $@ if $@; print Foo::len(); # prints 2
        In this version however you have to reuse the module, which can be problematic.
      Hardly practical if the module is a few thousands lines long, contaisn XS-code and calls in tons of other modules.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law