Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Building a Simple Perl Module Database

by chromatic (Archbishop)
on Apr 19, 2000 at 23:47 UTC ( #8069=snippet: print w/replies, xml ) Need Help??

Building a simple Perl Module Database

Suppose your latest Perl project makes ample use of homegrown modules. (For an example of this, see the Iaijutsu or Jellybean projects.) Because these are designed to work in a web environment, security is a big concern. However, the modules (or objects, as we tend to call them) also must be mutable. Developers need to be able to modify them slightly via the web interface.

One solution is to use eval() liberally. This involves a performance hit, however, in that each instance where the module is used, it must be recompiled (unless there are steps taken of which I am unaware). Since the module is more likely to be invoked at least ten times for each edit (a low estimate), this introduces a large penalty.

Another concern is security. Is it wise to allow the uid of the web server to have write access to the module directory? A clever invader could exploit this to do all sorts of nasty things. (Then again, allowing real-time editing of executable objects on a production web server does have similar risks.)

A better option, at least in my opinion, and for the Unix world, is a quick method which I devised recently.

Set up your web server and Perl content, running normally with restrictive permissions. Set up a module server process, as in the code section.

In your web server process, you will need to do a couple of things when you want to require() in a module. First, open a pipe for writing. It connects to $INPIPE in the module server. Second, write the name of the module you want, when you want it, to the pipe. Third, call request() on the name of the output pipe in the module server.

Yes, it really works -- require() thinks that a module pulled out of a database somewhere (or off of a network share or something else entirely more bizarre) is the same as a module read from a normal file. If it's valid Perl, it will work.

Please note that I have not set any handlers for SIGPIPE, which you will want to do in a production environment, nor have I given any suggestions for the appropriate locations for the pipes. Still, it is my hope that something here has given you a wacky idea which will prove useful.

use ModuleServer;

my $modpath = "/foo";     # path to where modules are expected
my $module = "";

while (1) {
    open (INPIPE, "> $INPIPE) || die "Can't open $INPIPE: $!";
    my $modname = <INPIPE>;
    close INPIPE;

# fetch module $modname from database or wherever into $module

    unless (-p $modname) {
        unlink $modname;
        system('mknod', $modname, 'p') 
            && die "Can't make pipe $modname: $!";

    open (OUTPIPE, "> $modpath$modname") 
        || die "Can't write to $modname: $!";
    print OUTPIPE $module;
    close OUTPIPE;
    sleep (1);
Replies are listed 'Best First'.
Coderefs in @INC (was: Building a Simple Perl Module Database)
by Aristotle (Chancellor) on Aug 25, 2002 at 04:23 UTC
    Did you know you can put coderefs in @INC?
    #!/usr/bin/perl -wl BEGIN { unshift @INC, \&magic_inc; sub magic_inc { print $_[1]; my $i; return \&magic_load if $_[1] eq "Foo/"; } my @src = ("print q(Foo::Bar loaded!);", "1;"); sub magic_load { return defined($_ = shift @src); } } use strict; use Data::Dumper; use Foo::Bar;
    The interface is somewhat obscure, but this close to the guts, you have to expect it.

    Makeshifts last the longest.

Re: Building a Simple Perl Module Database
by hiseldl (Priest) on Aug 24, 2002 at 00:24 UTC
    Nice idea! Would it be possible to post a short script that uses this technique as an example?


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://8069]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-03-02 21:40 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (63 votes). Check out past polls.