<?xml version="1.0" encoding="windows-1252"?>
<node id="293290" title="External program determing &quot;use bytes&quot; in module" created="2003-09-22 17:20:06" updated="2005-07-18 11:27:45">
<type id="1980">
snippet</type>
<author id="272239">
liz</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
[id://293020] was the reason for coming up with this (possible) abomination.  Slightly adapted here, with some more checks in case the same module is used from different locations with different settings.
&lt;P&gt;
Basically, this is a framework for a module that will either be compiled with "use bytes" or not, depending on a parameter passed (indirectly) to the import routine.  This turned out rather tricky to do.
&lt;P&gt;
So, in your program you would either do:
&lt;code&gt;
use Foo qw(usebytes);
&lt;/code&gt;
and the code of Foo.pm would be compiled with "use bytes" active.  Alternately, if you would do:
&lt;code&gt;
use Foo;
&lt;/code&gt;
in your program, then the source of Foo.pm would be compiled &lt;B&gt;without&lt;/B&gt; "use bytes".
&lt;P&gt;
I was wondering whether this could / should be made into a generally available module and/or whether this could be applicable to other pragma's.  Until I've made my mind up, I'll be leaving it here as this snippet.</field>
<field name="snippetcode">
&lt;CODE&gt;
package Foo;
require bytes; # make sure the module is loaded without (un)import

$bytes = 0;    # flag: whether bytes should be enforced
$evalled = 0;  # flag: whether rest of source has been compiled

sub import {
    if ($evalled++) {                   # we have evalled the rest before
        die qq{Can only have one setting of "use bytes" per run of Perl\n}
         if $bytes != ($_[1] eq "usebytes"); # different setting from before
    } else {                            # first time around
        $bytes = ($_[1] eq "usebytes"); # should bytes be enforced?
        local $/;                       # enable slurp mode
        eval &lt;DATA&gt;;                    # compile rest of source (after __DATA__)
        die $@ if $@;                   # die now if an error occurred
    }
};
1;                              # in order for require of Foo.pm to be successful
__DATA__                        # actual source of module starts here
BEGIN {bytes-&gt;import if $bytes} # activate bytes if flag set
sub new { bless {},shift }      # example routine
&lt;/CODE&gt;</field>
</data>
</node>
