in reply to Re: A module for creating "sane" URLs from "arbitrary" titles?
in thread A module for creating "sane" URLs from "arbitrary" titles?
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 ...
|
|---|