To create new wiki account, please join us on #znc at freenode and ask admins to create a wiki account for you. You can say thanks to spambots for this inconvenience.


From ZNC
Revision as of 17:28, 17 December 2011 by DarthGandalf (talk) (Module's NV)
Jump to: navigation, search

Modperl allows you to use modules written on perl.

This page describes module since ZNC 0.095. If you still need help on old modperl, look here.


First, you need to use ./configure with option --enable-perl.

To build modperl, you need some files produced by SWIG. They are not shipped with ZNC because of huge size (several megabytes). There're 2 ways to get them:

  1. Install swig. Needed files will be generated automatically when you run make in znc source dir to compile znc.
  2. Download and unpack these files to <znc-source>/modules/modperl/ ( and modperl.i should be in the same dir), and add option --disable-swig to ./configure


Loading and unloading of perl modules is similar to C++ modules. For example, you can use /znc loadmod or webadmin.

If you unload modperl, all perl modules are automatically unloaded too.


This user module takes no arguments.

Read loading modules to learn more about loading modules.

Writing new perl modules


Every perl module is file named like and is located in usual modules directories. The file must contain package with exactly the same name as the module itself. The module should be derived from ZNC::Module.


package perlexample;
use base 'ZNC::Module';

sub description {
    "Example perl module for ZNC"

sub OnChanMsg {
    my $self = shift;
    my ($nick, $chan, $msg) = @_;
    $self->PutModule("Hey, ".$nick->GetNick." said [$msg] on ".$chan->GetName);
    return $ZNC::CONTINUE;


If you want to have several perl packages inside your module, you should have them as subpackages of your module package. See Sockets section for example.

Description of the module is the return value from a sub description. All callbacks have the same name as in C++, and have the same arguments, but with reference to self before first argument, as usually in perl. The exception is callbacks which get vector<...> as last argument:

virtual void OnQuit(const CNick& Nick, const CString& sMessage, ''const vector<CChan*>& vChans'');
sub OnQuit {
    my ($self, $nick, $message, ''@chans'') = @_;
    for (@chans) {
        $self->PutIRC("PRIVMSG ".$_->GetName." :Poor ".$nick->GetNick." :(");

sub OnShutdown is used as destructor (instead of perl's DESTROY). OnShutdown is called when the module is going to be unloaded.

If a callback returns undef, a reasonable default is substituted. Remember that if execution comes to end of sub, last evaluated value is returned! If a callback dies/croaks, the default value is assumed too, the behavior of what happens to arguments is undefined. When a module callback should return CModule::EModRet, you can use values as $ZNC::CModule::CONTINUE or just $ZNC::CONTINUE.

Don't begin names of your member data fields with underscore (_) - some of them are used by modperl internally.

sub OnShutdown {
    my $self = shift;
    $self->{foo} = "foo"; # OK
    $self->{_bar} = "bar"; # Fail, probably will work but can randomly stop working.

ZNC C++ API can be found here. Most of it should just work for perl modules. The following text describes mostly features, differences and caveats.


All ZNC classes are accessible from perl with ZNC:: prefix. The exception is CString. All uses of CString by value are transparently translated to/from perl string scalars.

// C++
void Foo(const CString& s);
# perl
my $str = "bar";
ZNC::Foo($str); # OK
my $num = 42;
ZNC::Foo($num); # OK (only for ZNC >= 0.097)
ZNC::Foo("$num"); # OK
ZNC::Foo($num . ""); # OK
ZNC::Foo(ZNC::String->new($num)); # OK, see below about ZNC::String

If you need to call a function which gets CString by reference, and returns a value in it, there's a class named ZNC::String:

// C++
void Foo(CString& s) {
    s = "bar";
# perl
my $s = ZNC::String->new;
print $s->GetPerlStr; # prints 'bar' to stdout

As you see, to get normal string from ZNC::String there's a method GetPerlStr. You can construct non-empty ZNC::String using an argument to new. This constructor can get string scalars, integer scalars, float scalars.

When you implement a module hook which accepts CString&, no ZNC::String magic is needed, it just works:

sub OnChanMsg {
    my $self = shift;
    # @_ == (nick, channel, message)
    if ($_[0]->GetNick eq "Fish" || $_[2] eq "kwaa") {
        $_[2] = "moo!"

Module's NV

There're some issues with using std iterators from perl, so class CModule got new methods: GetNVKeys which returns list of names of all NV values of the module, and ExistsNV which checks if specified variable exists.

sub OnModCommand {
    my $self = shift;
    my @nvkeys = $self->GetNVKeys;
    if ($self->ExistsNV("foo")) {
        $self->SetNV("bar", $self->GetNV("foo"));

Also there's another interface for accessing NVs - perl hash. (But for big number of values it's slow)

sub OnModCommand {
    my $self = shift;
    my $nv = $self->NV;
    while (my ($key, $value) = each %$nv) {
    my @nvkeys = keys %$nv;
    $nv->{foo} = "bar";
    delete $nv->{foo};

Of course, you can use $self->{foo} for storing temporary values, but NV data is stored on disk.