I have the "URL was already created" thing on my to-do list as well, but I'm not sure that the module needs a callback. You can always do that check on the outside, and especially I'm unsure that incrementing the result string is what is desireable. For example I would like to append a counter such that img_1234 and img_1234 map to img_1234 (first) and img_1234_1 (second). Incrementing img_1234 to img_1235 will create an ugly and confusing rippling effect, as most of my images are sequentially numbered.

On the other hand, almost every situation I come up with has to tackle this problem - at least the two use cases I can see, generating URL fragments and (re)naming files according to contained tags.

So indeed, both functionalities would be useful, but I don't see how to do automatic counter (or whatever) generation through an API in a way that avoids duplicate_1_1_1 but still allows for img_1234 to become img_1234_1.

Passing in and using the duplicate detection callback is easy, but I don't see how it can be useful except if the duplicate callback returns the result to use instead. Basically, the use case with the explicit default callback would be:

my %fragment_exists; sanitize_name(sub {map { $_ . "_" . $fragment_exists{ $_ }++} @_ }, $title, ...);

I think the following idiom will be going into the documentation, together with the hint of sorting the fragments, and potentially stripping of anything that looks like a counter, to eliminate the _1_1_1 effect:

my %fragment_exists; my $fragment; my $duplicate = ''; do { $fragment = sanitize_name( "$title_" . $duplicate++ ); while( $fragment_exists{ $fragment }++ );

As sanitize_name will strip trailing underscores, it's easy to add a counter to the end that way, even if that counter starts numbering the first duplicate with _1 instead of _2 ...


In reply to Re^2: A module for creating "sane" URLs from "arbitrary" titles? by Corion
in thread A module for creating "sane" URLs from "arbitrary" titles? by Corion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.