Author image Bartosz Jarzyna
and 1 contributors


Web::Sitemap - Simple way to generate sitemap files with paging support


        use Web::Sitemap;

        my $sm = Web::Sitemap->new(
                output_dir => '/path/for/sitemap',

                ### Options ###

                temp_dir    => '/path/to/tmp',
                loc_prefix  => '',
                index_name  => 'sitemap',
                file_prefix => 'sitemap.',

                # mark for grouping urls
                default_tag => 'my_tag',

                # add <mobile:mobile/> inside <url>, and appropriate namespace (Google standard)
                mobile      => 1,

                # add appropriate namespace (Google standard)
                images      => 1,

                # additional namespaces (scalar or array ref) for <urlset>
                namespace   => 'xmlns:some_namespace_name="..."',

                # location prefix for files-parts of the sitemap (default is loc_prefix value)
                file_loc_prefix  => '',

                # specify data input charset
                charset => 'utf8',

                move_from_temp_action => sub {
                        my ($temp_file_name, $public_file_name) = @_;

                        # ...some action...
                        # default behavior is
                        # File::Copy::move($temp_file_name, $public_file_name);



        # When adding a new portion of URL, you can specify a label for the file in which these will be URL

        $sm->add(\@url_list1, tag => 'articles');
        $sm->add(\@url_list2, tag => 'users');

        # If in the process of filling the file number of URL's will exceed the limit of 50 000 URL or the file size is larger than 50MB, the file will be rotate

        $sm->add(\@url_list3, tag => 'articles');

        # After calling finish() method will create an index file, which will link to files with URL's



This module is an utility for generating indexed sitemaps.

Each sitemap file can have up to 50 000 URLs or up to 50MB in size (after decompression) according to Any page that exceeds that limit must use sitemap index files instead.

Web::Sitemap generates a single sitemap index with links to multiple sitemap pages. The pages are automatically split when they reach the limit and are always gzip compressed. Files are created in form of temporary files and copied over to the destination directory, but the copy action can be hooked into to change that behavior.


Web::Sitemap only provides OO interface.



        my $sitemap = Web::Sitemap->new(output_dir => $dirname, %options);

Constructs a new Web::Sitemap object that will generate the sitemap.

Files will be put into output_dir. This argument is required.

Other optional arguments include:

  • temp_dir

    Path to a temporary directory. Must already exist and be writable. If not specified, a new temporary directory will be created using File::Temp.

  • loc_prefix

    A location prefix for all the urls in the sitemap, like ''. Defaults to an empty string.

  • index_name

    Name of the sitemap index (basename without the extension). Defaults to 'sitemap'.

  • file_prefix

    Prefix for all sitemap files containing URLs. Defaults to 'sitemap.'.

  • default_tag

    A default tag that will be used for grouping URLs in files when they are added without an explicit tag. Defaults to 'pages'.

  • mobile

    Will add a mobile namespace to the sitemap files, and each URL will contain <mobile:mobile/>. This is a Google standard. Disabled by default.

  • images

    Will add images namespace to the sitemap files. This is a Google standard. Disabled by default.

  • namespace

    Additional namespaces to be added to the sitemap files. This can be a string or an array reference containing strings. Empty by default.

  • file_loc_prefix

    A prefix that will be put before the filenames in the sitemap index. This will not cause files to be put in a different directory, will only affect the sitemap index. Defaults to the value of loc_prefix.

  • charset

    Encoding to be used for writing the files. Defaults to 'utf8'.

  • move_from_temp_action

    A coderef that will change how the files are handled after successful generation. Will be called once for each generated file and be passed these arguments: $temporary_file_path, $destination_file_path.

    By default it will copy the files using File::Copy::move.


        $sitemap->add(\@links, tag => $tagname);

Adds more links to the sitemap under $tagname (can be ommited - defaults to pages or the one specified in the constructor).

Links can be simple scalars (URL strings) or a hashref. See "new" in Web::Sitemap::Url for a list of possible hashref arguments.

Can be called multiple times.



Finalizes the sitemap creation and calls the function to move temporary files to the output directory.


Support for Google images format

Format 1

                loc => '',
                images => {
                        caption_format => sub {
                                my ($iterator_value) = @_;
                                return sprintf('Vasya - foto %d', $iterator_value);
                        loc_list => [

Format 2

                loc => '',
                images => {
                        caption_format_simple => 'Vasya - foto',
                        loc_list => ['', '']

Format 3

                loc => '',
                images => {
                        loc_list => [
                                { loc => '', caption => 'image #1' },
                                { loc => '', caption => 'image #2' },
                                { loc => '', caption => 'image #3' },
                                { loc => '', caption => 'image #4' }


Mikhail N Bogdanov <mbogdanov at >


In no particular order:

Ivan Bessarabov

Bartosz Jarzyna (@brtastic)


This module and all the packages in this module are governed by the same license as Perl itself.