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

Hi guys,

I know there's a way to use a module ONLY for a small part of my code as opposed putting the use statement up there. The reason is that use bigint is making my code extremely slower and i only need it for a very small part of it. If i remember correctly it's something like begin use module_name and after some code end use module_name.Can anyone help me out? Also, do you know of any other library that i can use instead of bigint?

Thanks

Replies are listed 'Best First'.
Re: using a module for a portion of the code only
by kennethk (Abbot) on Sep 06, 2009 at 15:14 UTC
    It might do you some good to read through I know what I mean. Why don't you?. For example, you ask us what library you can use instead of bigint but don't tell us what you are trying to do. And please post code when you have it.

    In any case, assuming you want to use bigint in a highly localized fashion, you can either use a use and no block to control how the packages are used or you can just wrap the code that needs bigint in a block, thus localizing the use. Note that the answer to your question is right in the code examples in the bigint documentation - Perl documentation is your friend.

Re: using a module for a portion of the code only
by FunkyMonk (Bishop) on Sep 06, 2009 at 15:11 UTC
    I presume you mean no bigint;

    You could have found that on your own had you bothered to look at the documentation for either use or bigint.

Re: using a module for a portion of the code only
by ikegami (Patriarch) on Sep 06, 2009 at 17:01 UTC

    use bigint; is lexically scoped. You can limit what it effects by putting curlies around it.

    no bigint; cancels bigint's effects. It is also lexically scoped.

    Another way of limiting bigint's effect — turning numerical constants into Math::BigInt objects — is to avoid using bigint completely and create Math::BigInt objects for the appropriate constants manually.

    For example, say you're starting with the following:

    >perl -MO=Concise,-exec -Mbigint -e"my $big = 1234; for (1..4) { foo($ +big) }" ... 7 <0> pushmark s 8 <$> const[RV \HASH] s <-- for loop indexes are needlessly 9 <$> const[RV \HASH] s <-- BigInt objects. a <#> gv[*_] s b <{> enteriter(next->h last->k redo->c) lKS/8 ...

    Let's selectively make $big a BigInt to avoid making the loop indexes objects:

    >perl -MO=Concise,-exec -MMath::BigInt -e"my $big = Math::BigInt->new( +'1234'); for (1..4) { foo($big) }" ... b <0> pushmark s c <$> const[IV 1] s <-- Much better d <$> const[IV 4] s <-- e <#> gv[*_] s f <{> enteriter(next->l last->o redo->g) lKS/8 ...