Charles Engelke's Blog

June 25, 2003

Writing my first plug-in

Filed under: blosxom — Charles Engelke @ 9:01 pm

Okay, I wanted to write a plug-in to turn a single instance of the Blosxom
script into one that behaves differently depending on the URL used to access
it. I’m assuming that there are multiple ScriptAlias directives in the Apache
configuration, each pointing a different URL at the same script. If the URL
is /charles, the script should use configuration values appropriate for user
charles. If the URL is /bobd, the script should use configuration values for
that user. The configuration variables are blog_title,
blog_description, and datadir in the Blosxom script.

Blosxom’s plug-in architecture is really elegant. The main program scans
the designated plug-in directory for files. Every time it finds a file
(named multiuser for example), it requires that file. That makes
Perl read the file and compile it. Then, later on, Blosxom calls subroutines
in each plug-in file at various appropriate times. The only subroutine we care
about right now is called start. Blosxom calls the start subroutine
in each plug-in early in its execution.

The multiuser plug-in puts commands in its start subroutine
to correct the values of blog_title,
blog_description, and datadir in the main Blosxom script. That’s
just three simple assignment statements. Of course, which values are assigned
will depend on which URL was used to invoke the script.

There’s one more Perl feature that’s important here: packages. A
Perl package is a name space. When Blosxom calls the start subroutine,
it has to know how to call our subroutine, not one of the same name in
one of the other plug-ins. Blosxom assumes that all of our code is in a
package with the same name as the plug-in file. Remember, that name is multiuser
in our example. Names in the main program would usually be in the main
package, but Blosxom explicitly puts the program in a package named blosxom.
Remember that, we’ll use it. Note that this also means we can’t call our
plug-in blosxom, since it would conflict with the already defined

Okay, here’s the code. We can start with comments, but the first executable
line will declare the package. The last executable line has to evaluate to
a true value, which in Perl is almost anything that isn’t 0 or blank. The
skeleton looks like:

package multiuser;
#program code here

The program code needs to see what the URL is, and assign appropriate
values to three variables in the Blosxom script. The URL is available in the
REQUEST_URI environment variable, so we now have:

package multiuser;
sub start {
my $username = $ENV{REQUEST_URI};
$username =~ s/\W//g; # Get rid of leading /, etc.
if ($username eq ‘charles’) {
$blosxom::blog_title = “Charlie’s Blog”;
$blosxom::blog_description = “This is Charlie’s Blog”;
$blosxom::datadir = ‘/home/charles/blog’;
elsif ($username eq ‘bobd’) {
$blosxom::blog_title = “Bob’s Blog”;
$blosxom::blog_description = “This is Bob’s Blog”;
$blosxom::datadir = ‘/home/bobd/blog’;

That’s all there is. We don’t have to change the main script file at
all, just put this plug-in into the right directory.


Blog at

%d bloggers like this: