The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


HTML::Highlighter - PSGI middleware to highlight text in an HTML response


    use Plack::Builder;
    use HTML::Highlighter;

    # highlight the "search" query param

    builder {
      enable "+HTML::Highlighter", param => "search";

    # or highlight the user stored in session

    builder {
      enable "+HTML::Highlighter", callback => sub {
        my $env = shift;
        $env->{'psgix.highlight'} = $env->{'psgix.session'}{user};


The HTML::Highlighter module is a piece of PSGI middleware that will inspect an HTML response and highlight parts of the page based on a query parameter or other request data. This is very much like what Google does when you load a page from their cache. Any text that matches your original query is highlighted.

    <span class="highlight">[matching text]</span>

If no param or callback are provided to HTML::Highlighter, it will look for commonly used search parameters (e.g. q, query, search, and highlight.)

This module also includes a javascript file called highlighter.js which gives you a class with methods to jump (scroll) through the highlights.



This option allows you to specify a query parameter to be used for the highlighting. For example, if you specify "search" as the param, each response will look for a query parameter called "search" will highlight that value in the response.


This option lets you specify a function that will be called on each request to generate the text used for highlighting. The function will be passed the $env hashref, and should set 'psgix.highlight' on it. This value will be used for the highlighting. This could be useful if you want to highlight a username that is stored in a session, or something similar.





Copyright (C) 2010 Lee Aylward <leedo at>, all rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Lee Aylward, <>