Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: conditional enable use bytes for a whole module at compiletime?

by Random_Walk (Prior)
on Nov 19, 2004 at 13:33 UTC ( [id://408989]=note: print w/replies, xml ) Need Help??


in reply to conditional enable use bytes for a whole module at compiletime?

I think you will find there is an issue here with use and BEGIN.

use is equivalent to BEGIN { require Module; import Module LIST; } and a BEGIN block is run as soon as possible. This effectively means when the closing brace is encountered. In your above code the implied BEGIN block in use will be run before the surrounding BEGIN block and its if logic is evaluated. Not What you want.

I read a node recently that explained it all far better than I can, I will link it here as soon as I can find it

Here tachyon gives an ellegant eaxample of the futility of wrapping use in an if

</code>

Cheers,
R.

  • Comment on Re: conditional enable use bytes for a whole module at compiletime?
  • Download Code

Replies are listed 'Best First'.
Re^2: conditional enable use bytes for a whole module at compiletime?
by bpphillips (Friar) on Nov 19, 2004 at 13:55 UTC
    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 :-)
Re^2: conditional enable use bytes for a whole module at compiletime?
by borisz (Canon) on Nov 19, 2004 at 13:45 UTC
    Thanks, but the BEGIN has nothing todo with my problem. As you see, the surrounding BEGIN includes all the use statements. I have done it this way, to simulate use X. Since the posting is one file ;-)
    Boris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://408989]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found