Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3275142
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:06:44+00:00 2026-05-17T19:06:44+00:00

Web frameworks such as Rails and Django has built-in support for slugs which are

  • 0

Web frameworks such as Rails and Django has built-in support for “slugs” which are used to generate readable and SEO-friendly URLs:

  • Slugs in Rails
  • Slugs in Django

A slug string typically contains only of the characters a-z, 0-9 and - and can hence be written without URL-escaping (think “foo%20bar”).

I’m looking for a Perl slug function that given any valid Unicode string will return a slug representation (a-z, 0-9 and -).

A super trivial slug function would be something along the lines of:

$input = lc($input),
$input =~ s/[^a-z0-9-]//g;

However, this implementation would not handle internationalization and accents (I want ë to become e). One way around this would be to enumerate all special cases, but that would not be very elegant. I’m looking for something more well thought out and general.

My question:

  • What is the most general/practical way to generate Django/Rails type slugs in Perl? This is how I solved the same problem in Java.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:06:45+00:00Added an answer on May 17, 2026 at 7:06 pm

    The slugify filter currently used in Django translates (roughly) to the following Perl code:

    use Unicode::Normalize;
    
    sub slugify($) {
        my ($input) = @_;
    
        $input = NFKD($input);         # Normalize (decompose) the Unicode string
        $input =~ tr/\000-\177//cd;    # Strip non-ASCII characters (>127)
        $input =~ s/[^\w\s-]//g;       # Remove all characters that are not word characters (includes _), spaces, or hyphens
        $input =~ s/^\s+|\s+$//g;      # Trim whitespace from both ends
        $input = lc($input);
        $input =~ s/[-\s]+/-/g;        # Replace all occurrences of spaces and hyphens with a single hyphen
    
        return $input;
    }
    

    Since you also want to change accented characters to unaccented ones, throwing in a call to unidecode (defined in Text::Unidecode) before stripping the non-ASCII characters seems to be your best bet (as pointed out by phaylon).

    In that case, the function could look like:

    use Unicode::Normalize;
    use Text::Unidecode;
    
    sub slugify_unidecode($) {
        my ($input) = @_;
    
        $input = NFC($input);          # Normalize (recompose) the Unicode string
        $input = unidecode($input);    # Convert non-ASCII characters to closest equivalents
        $input =~ s/[^\w\s-]//g;       # Remove all characters that are not word characters (includes _), spaces, or hyphens
        $input =~ s/^\s+|\s+$//g;      # Trim whitespace from both ends
        $input = lc($input);
        $input =~ s/[-\s]+/-/g;        # Replace all occurrences of spaces and hyphens with a single hyphen
    
        return $input;
    }
    

    The former works well for strings that are primarily ASCII, but falls short when the entire string is formed of non-ASCII characters, since they all get stripped out, leaving you with an empty string.

    Sample output:

    string        | slugify       | slugify_unidecode
    -------------------------------------------------
    hello world     hello world     hello world
    北亰                            bei-jing
    liberté         liberta         liberte
    

    Note how 北亰 gets slugifies to nothing with the Django-inspired implementation. Note also the difference the NFC normalization makes — liberté becomes ‘liberta’ with NFKD after stripping out the second part of the decomposed character, but would becomes ‘libert’ after stripping out the re-assembled ‘é’ with NFC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Web frameworks such as Rails and Django has built-in support for slugs which are
There seem to be many excellent web frameworks for Python. Has anyone used any
Do the major web application frameworks (Rails, Django, etc) have libraries that provide functionality
Is a web framework, such as Django and Ruby on Rails, simply a way
Are there any good JavaScript frameworks out there which primary audience is not web
I'm wondering if there's such a thing as Django-like ease of web app development
This is a general question about how limiting are web development frameworks such as
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
What semantic web frameworks are there, and what are the advantages / disadvantages of
I'm evaluating web application frameworks for a hobby project I'm starting, and am beginning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.