Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

conditional enable use bytes for a whole module at compiletime?

by borisz (Canon)
on Nov 19, 2004 at 11:58 UTC ( [id://408981]=perlquestion: print w/replies, xml ) Need Help??

borisz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I like to enable use bytes for a whole module, only if a condition is true. But since use bytes is out of scope, outside of the if, it has no effect. Sure I know I can wrap every function with the test, but I hope for a easy solution at the top of the module. Here is a example, the desired output is 3.
BEGIN { package Y; $VERSION = 0.1; 1; package X; if ( $Y::VERSION < 1 ) { # How to enable use bytes for the whole modue X? # with condition $Y::VERSION in mind? use bytes; } sub l{ return length shift } # large module follows 1; } package main; no bytes; $x = chr(3456); print X::l($x);
Boris

Replies are listed 'Best First'.
Re: conditional enable use bytes for a whole module at compiletime?
by ikegami (Patriarch) on Nov 19, 2004 at 15:50 UTC

    There's a module named if which was written specifically to do what you want. It comes with Perl.

    script.pl

    use strict; use warnings; # Initialized in BEGIN so the use can see $VERSION. use vars qw( $VERSION ); BEGIN { $VERSION = 0.1; } #use if $VERSION < 1, 'bytes'; use if $VERSION < 1, 'Proof' => 'try1'; use if $VERSION > 1, 'Proof' => 'try2';

    Proof.pm

    package Proof; sub import { print('used at ', $_[1], $/); } 1;
Re: conditional enable use bytes for a whole module at compiletime?
by Random_Walk (Prior) on Nov 19, 2004 at 13:33 UTC

    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.

      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 :-)
      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
Re: conditional enable use bytes for a whole module at compiletime?
by broquaint (Abbot) on Nov 19, 2004 at 15:49 UTC
    Since you simply want to conditionally include a lexically scoped module at compile-time it sounds like the if module will do the job. If adapted to your example code it looks to work just fine:
    BEGIN { $Y::VERSION = 0.1 } { package X; use if ( $Y::VERSION < 1 ), "bytes"; sub l { return length shift } } no bytes; print X::l( chr 3456 ); __output__ 3
    HTH

    _________
    broquaint

      Thats what I searched for!. Thanks.
      Boris
Re: conditional enable use bytes for a whole module at compiletime?
by monsieur_champs (Curate) on Nov 19, 2004 at 13:51 UTC

    I think you can say

    BEGIN { package Y; $VERSION = 0.1; 1; package X; if ( $Y::VERSION < 1 ) { # How to enable use bytes for the whole modue X? # with condition $Y::VERSION in mind? ## Not use()'d anymore. # use bytes; require bytes; }

    And this will do what you mean.

    Note: this is untested code, as always.

      Hi, no, you think, that my desire is to load the module on a condition, this is not true. I like to enable it on a condition in another scope. For example, this works fine, but I want use bytes only if $Y::VERSION  < 1.
      BEGIN { package Y; $VERSION = 0.1; 1; package X; use bytes; sub l{ return length shift } 1; } package main; no bytes; $x = chr(3456); print X::l($x);
      Boris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://408981]
Approved by Happy-the-monk
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found