http://qs1969.pair.com?node_id=343785

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

Hi...
my module needs for exactly one function the module Math::XOR. So i thought includding it by "require Math::XOR" in the scope of the function since the function is not used very much.
but if i use "require", perl says that it can't find xor_buf. I ve tried Math::XOR::xor_buf also with no success.
Any hints?

Replies are listed 'Best First'.
Re: require Math::XOR
by tilly (Archbishop) on Apr 08, 2004 at 23:32 UTC
    Here are several solutions.

    The ^ operator documented in perlop looks identical to xor_buf, so you don't need it at all.

    Answering your original question, check whether this code to do a delayed use is cross-platform, but I think it should be:

    unless ($INC{"Math/XOR.pm"}) { require Math::XOR; Math::XOR->import(); }
    Or you can just live with the fact that require by itself doesn't do an import, and you can fully qualify the function name. Or you can do it manually as follows:
    require Math::XOR; *xor_buf = \&Math::XOR::xor_buf unless defined &xor_buf;
    Update: PodMaster is correct. I added the .pm that I was missing.

    UPDATE 2: Gah. I missed :: to / as well. I swear I was thinking it as I typed it...

      I think you mean
      unless($INC{'Math/XOR.pm'}) { ...
      (:

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re: require Math::XOR
by saintmike (Vicar) on Apr 08, 2004 at 22:55 UTC
    require Math::XOR; print Math::XOR::xor_buf("ab", "cd");
    seems to work fine.
Re: require Math::XOR
by Anomynous Monk (Scribe) on Apr 08, 2004 at 23:23 UTC
    use normally does a require and import; if you want to be able to call xor_buf without qualification, you need to say Math::XOR::->import() after the require.

    You may still run into problems with perl parsing things differently than if it had known about the xor_buf sub at compile time. I'm guessing Math::XOR::xor_buf didn't work because of that (in which case make sure it looks like a function call to perl, e.g. &Math::XOR::xor_buf("a","b") with & and ()) or because you got the case of some part of the name wrong.

      I was going to say that calling import multiple times under warnings could trigger spurious warnings. It can, but it doesn't in this case.

      Live and learn.

Re: require Math::XOR
by Anomynous Monk (Scribe) on Apr 08, 2004 at 23:46 UTC
    Hrmph; Math::XOR::xor_buf($a,$b) does nothing different than "$a"^"$b" (except that it ignores anything in $b beyond the end of $a, and is broken for utf8 data, and doesn't try to convert the input to a string if given a non-string (e.g. an overloaded object, or a numeric-only scalar)).