Ok, I guess I missed that particular point. Well, I think it is possible, but it's rather yucky and it suffers from the same problems that AutoLoader and the likes have. Basically, you compile your code in a two step process. First to compile the "import" routine. Then, when the import routine is called, the rest of the source is compiled Consider module Foo:
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 {
unless ($evalled++) { # we haven't evalled the rest
+before
$bytes = ($_[1] eq "usebytes"); # should bytes be enforced?
local $/; # enable slurp mode
eval <DATA>; # compile rest of source (afte
+r __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->import if $bytes} # activate bytes if flag set
sub new { bless {},shift } # example routine
Then, in an example program, you would do:
use Foo 'usebytes'; # or something else to not use bytes
my $foo = Foo->new;
Hope this will help you in your quest.
Liz
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|