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

I have a group of perl scripts on a single server that access and perform different actions on a website using the same basic framework. I have separated these chunks into subroutines. At present the subroutine code chunks are simply copy and pasted into each script that needs them. I realise that this would ideal for creating a package/module for my own use:
package MyPackage; sub doSomething { #do something, mutley }
and then use this just as with a 'proper' module from CPAN:
use MyStuff; my $a = "foo"; my $b = doSoemthing($a);
However, I have basic server access only and cannot install new modules on the CGI server. So my question is, is there a way to do something similar to give me a reusable code chunk that can be accessed from multiple scripts without creating and installing a module?

Dan

Replies are listed 'Best First'.
Re: Creating a pseudo-module/package on a remote (CGI) server?
by jettero (Monsignor) on Dec 21, 2006 at 09:21 UTC

    You could just build your Module.pm files along side your scripts and load them from there. You don't need to install them in the usual perl libdirs... In fact, unless I'm intending to distribute the modules for others normally don't put them in a different directory at all. Really, you can put them anywhere, as long as you -I the dir or add them to @INC before you use them — usually requres a BEGIN block..

    Is there some file-filter machine that's preventing you from uploading anything with a .pm extension? You could even call them .pl files and require them by name.

    -Paul

      Just one extra tip in addition to jettero's advice: The easiest way to add another directory to your library path is to put use lib '/path/to/directory/with/modules'; at the top of your script. After you do that, you can use your own modules as if they were installed in the normal place -- you can even upload pure-perl modules from CPAN there and have them work fine.

      Note that you probably don't want to just put the modules alongside your script, though -- it's a good idea to put your modules in a directory that's not web-accessible so there is no risk of visitors to your site being able to view their source code.

      Aha! Thanks for your reply. My scripts are in "/cgi-bin" but I can access "/" (non web-accessible) so I could create a subdir there called modules and upload my newly created *.pm files just fine. I didn't realise that I could use modules in this way - everything I read stated I had to install them first, which I can't do as stated. I shall have a look at BEGIN, @INC and "-|" as suggested, thanks!

      But basically, you're saying I can simply upload them to a subdirectory as .pm, specify the modules folder local path as a lib, and then use "use" etc to access them exactly as if they were an installed module? Great!

        You may note that . disappears from @INC if you're running with taint checking, which you are forced to have when running as root ...

        Not that you'd ever dream of running your webserver or CGI scripts as root... but just so you know

        use lib 'lib'; seems a farily nice alternative (you can stick it at the top of your scripts, right where you've got use strict; use warnings;

        Throw those suckers into a skeleton script and you're away...

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: Creating a pseudo-module/package on a remote (CGI) server?
by siva kumar (Pilgrim) on Dec 21, 2006 at 09:58 UTC
    Just an extra tip in addition to jettero, dirving advice. Add your custom directories to perl's search path by using unshift(@INC,"/custom dir path");
      Could you just explain this a bit more please and how it differs to specifiying the path via "lib"??

        the man says:

        Saying
        use lib LIST;
        is almost the same as saying
        BEGIN { unshift(@INC, LIST) }
        For each directory in LIST (called $dir here) the lib module also checks to see if a directory called $dir/$archname/auto exists. If so the $dir/$archname directory is assumed to be a corresponding architecture specific directory and is added to @INC in front of $dir.

        ...in other words use lib is far cleaner, and does a bundle more bookkeeping, for you ... (also, it does it in a BEGIN - which you may forget should should you manipulate @INC directly

        sorry of the <code>'s are in silly places...

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: Creating a pseudo-module/package on a remote (CGI) server?
by cub.uanic (Acolyte) on Dec 21, 2006 at 15:34 UTC
    You also could use
    use FindBin qw/ $RealBin /; use lib "$RealBin/lib";
    or
    use FindBin qw/ $RealBin /; use lib "$RealBin/../lib";
    depending on your directory layout.
    See also FindBin
    HTH