Developers > Programming > Example Plug-ins

Example Plug-ins

Example 1

This first example is ExSite's equivalent of "Hello World". It inherits from BaseDCD, which allows us to skip over many of the basic DCD methods in our class.

package Modules::HelloWorld;
use strict;
use Modules::BaseDCD;
use vars (@ISA);
@ISA = qw(Modules::BaseDCD);

sub write {
my ($this,$options) = @_;
# we accept the $options string, but ignore it
return "Hello World!";
}

# we also say hi to admins!

sub ioctl {
my ($this,$request) = @_;
if ($request eq "ControlPanel") {
return \&ctrl_panel;
}
}

sub ctrl_panel {
my $this = shift;
return "Hello Underworld!";
}

1;

Example 2

This example is slightly more complicated. It can be used to inspect the server and application environment for debugging purposes. Note that it is written in Object-Oriented Perl, and uses the ExSite::Input class library to share input with other DCDs. In addition to the three standard DCD methods, there is also a class constructor (new()), and a private method (showhash()). It is not using BaseDCD and so has to define all module methods.

package Modules::Debug;

use strict;
use ExSite::Input;

# new : called implicitly once per page to instantiate the applet,
# when the DCD is first invoked

sub new {
my $this = shift;
my $obj = {};
my $class = ref($this) || $this;
bless $obj, $class;

# call the read method (no special args) when we create the DCD
# so that we don't have to invoke read() explicitly.
$obj->read;

return $obj;
}

sub read {
my ($this,$opt) = @_; # note: $opt not used
# use Input class to grab any form input. The Input class saves
# all input so that it can be re-used by other DCDs.
my $in = new ExSite::Input;

# retrieve and parse any data passed by POST method,
# save the data in this object
$this->{post_data} = $in->post;

# retrieve and parse any data passed by GET method
# save the data in this object
$this->{get_data} = $in->get;
}

sub write {
my ($this,$options) = @_;
# allow user to specify what info they want to see in the option string
if ($options =~ /POST/) {
return $this->showhash($this->{post_data});
}
if ($options =~ /GET/) {
return $this->showhash($this->{get_data});
}
else {
# by default, show the server environment
return $this->showhash(\%ENV);
}
}

sub ioctl {
my ($this,$request) = @_;

# The module name response is not necessary, since it is the same
# as the DCD name, which is used by default.

if ($request eq "ModuleName") { return "Debug"; }

# There is no real control panel for this module, but for the sake
# of illustration, we will display the server environment as a
# "control panel". We do this simply by passing a reference to
# the write() method, since that's exactly what it does with no
# special args.

if ($request eq "ControlPanel") {
return \&write;
}
}

# a private DCD routine that cannot be called externally from the
# administrator WebTop or from content management tags.

sub showhash {
my ($this,$href) = @_;
my $out;
# displays the keys and values of a hash
while (my ($key,$val) = each %$href) {
$out .= "$key = $val<br>\n";
}
return $out;
}

1;

Topics