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

Hi,

I have found a CPAN module to be very useful and I call it from my code. However, my code needs to run on ANY workstation at my site (and at other sites), not just my own workstation, which has the module installed. I cannot convince the sysadmin to install the module everywhere.

Is there a way to just cut/paste the module's code inside of my Perl code? Of course I would credit the author in the comments.

Or could I put the module's code in the same directory as my Perl code?

I'd love to see an example.

Thanks!
  • Comment on How to use a module without installing it?

Replies are listed 'Best First'.
Re: How to use a module without installing it?
by FunkyMonk (Bishop) on Aug 03, 2009 at 22:19 UTC
Re: How to use a module without installing it?
by toolic (Bishop) on Aug 03, 2009 at 22:28 UTC
    Is there a way to just cut/paste the module's code inside of my Perl code? ... I'd love to see an example.
    Yes. Here is a trivial example:
    Foo::bar(); package Foo; sub bar {print "bar: hello\n"}

      In the more general case, it's usually a good idea to put an extra block (pair of curlies) around each included module's code section. This is in order to emulate the extra scope implied when having the code in a separate file. I.e.

      ... { package Foo; sub bar {print "bar: hello\n"} } { package Bar; ... }

      Of course, it won't make a difference in this simple case, but with more complex code, subtle bugs due to scoping issues might result without the extra braces.

      Also, depending on whether the included code needs to be compiled/run prior to the rest of the script (as a normal use would do), you might need to put the code in a BEGIN {} block...

        Hi,

        I tried your technique for the module I needed - let's call it "A::B".
        I pasted the A::B module above my code, inside curly braces {}
        The "A::B" code "require"s the parent code "A", which I pasted above it, inside curly braces {}
        and the "A" code "require"s code "A::C", which I pasted above it, inside curly braces {}

        Now I get the error message:

        Can't locate A/C.pm in @INC

        Any ideas?
Re: How to use a module without installing it?
by halfcountplus (Hermit) on Aug 03, 2009 at 23:10 UTC
    Or could I put the module's code in the same directory as my Perl code?
    Yes, if the module is in the same directory, you can just do do this, presuming the module is called Whatever.pm:
    use Whatever;
    Notice that is not it's normal canonical name (eg, Special::Whatever). You should test this way to be sure it will work with any particular module, of course.

    If you put it in some other directory you can do this:
    BEGIN { push @INC,"/some/directory"; }
    And use "use" the same way.
Re: How to use a module without installing it?
by cdarke (Prior) on Aug 04, 2009 at 03:05 UTC
    Some modules have an XS component which generate a DLL or .so file (depending on the OS). You will also need that if the module is of that type. On Windows it searches the current directory for the DLL, but on UNIX or Linux it uses environment variable LD_LIBRARY_PATH to find it.

    Note, when placing a module in the same directory as your script, it is easy to assume this is the current directory - it might not be. @INC by default includes the current directory, not (necessarily) the directory containing your script. It will not contain your home directory unless you put the home directory in @INC yourself. You might have to code around that in a BEGIN block. See How do I get the full path to the script executing? and lib.
    Update: added home directory comments
Re: How to use a module without installing it?
by Bloodnok (Vicar) on Aug 04, 2009 at 11:47 UTC
    An alternative to the suggestions made thus far would be to create a networked file system (visible to all intending using machines) and install the module therein - creating access to the external library, on those machines, via $PERl5LIB.

    Self-evidently this has its drawbacks - not the least of which is the reliance on the network remaining functional - but, IMO, in most cases, the benefits far outweigh the drawbacks - the most fundamental of which is the disproportionately large maintenance overhead involved in maintaining multiple copies of the same source.

    A user level that continues to overstate my experience :-))