Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
if only blocks were avoidable... 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.

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.

So, in your program you would either do:

use Foo qw(usebytes);
and the code of Foo.pm would be compiled with "use bytes" active. Alternately, if you would do:
use Foo;
in your program, then the source of Foo.pm would be compiled without "use bytes".

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.

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 bef +ore die qq{Can only have one setting of "use bytes" per run of Per +l\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 <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

In reply to External program determing "use bytes" in module by liz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-20 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found