1. Writing Hooks

Hooks are written either in cgi-bin/ljlib.pl file, or in a separate site-specific library file. Site-specific hooks are defined by the function LJ::register_hook(), which takes two arguments: the name of the hook, and the hook's perl coderef.

Most hook coderefs are passed a single hash of various objects as their argument, though there are a few exceptions. Consult Section 2, “Hooks Reference” for more information.

Example 19.1. Sample hook: userinfo_html_by_user

To give you an idea of how a hook is set up, a smaller hook is sampled below:

LJ::register_hook("userinfo_html_by_user", sub {
    my $o = shift;
    my $r = $o->{'ret'};
    my $u = $o->{'u'};
    return unless (LJ::get_cap($u, "normal"));
    $$r .= "<img src='$LJ::IMGPREFIX/talk/sm08_star.gif' width='20' height='18' alt='&#8902;' style='vertical-align: middle; border: 0;' />";
        });

$o is the argument hash that is passed to the hook's subroutine, and consists of the scalar reference member $o->{'r'} and the user object member $o->{'u'}. The subroutine then checks to see if the user is part of the capability class “normal”, and if so it will pass the HTML image directive as the scalar reference. Now when someone loads a user information page for a “normal” user, a small star will appear next to their username and userid.