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 630977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:54:51+00:00 2026-05-13T19:54:51+00:00

I have encountered some strange Perl behavior: using a Posix character class in a

  • 0

I have encountered some strange Perl behavior: using a Posix character class in a regexp completely alters the sort order for the resulting strings.

Here is my test program:

sub namecmp($a,$b) {
  $a=~/([:alpha:]*)/;
  # $a=~/([a-z]*)/;
  $aword= $1;

  $b=~/([:alpha:]*)/;
  # $b=~/([a-z]*)/;
  $bword= $1;
  return $aword cmp $bword;
};

$_= <>;
@names= sort namecmp split;
print join(" ", @names), "\n";

If you change to the commented-out regexp’s using [a-z], you get the normal, lexicographic sort order. However, the Posix [:alpha:] character class yields some weird-ass sort order, as follows:

$test_normal
aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cbb
aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cbb

$test_posix
aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cbb
baa bab bac bba bbb bbc bca bcb bcc caa cbb aba abb abc aca acb acc aab aac aaa

My best guess is that the Posix character class is activating some kind of locale stuff I’ve never heard of and didn’t ask for. I suppose the logical reaction to “doctor, doctor, it hurts when I do this!” is, “well, don’t do that, then!”.

But, can anyone tell me what’s happening here, and why? I’m using perl 5.10, but I believe it also works under perl 5.8.

  • 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-13T19:54:52+00:00Added an answer on May 13, 2026 at 7:54 pm

    The character class [:alpha:] represents alpha characters in Perl regular expressions, but the square brackets do not mean what they normally do in regular expressions. So you need:

    $a=~/([[:alpha:]]*)/;
    

    This is mentioned in perlre:

    The POSIX character class syntax

    [:class:]
    

    is also available. Note that the [ and ] brackets are literal; they must always be used within a character class expression.

    # this is correct:
    $string =~ /[[:alpha:]]/;
    
    # this is not, and will generate a warning:
    $string =~ /[:alpha:]/;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered a strange behavior with a simple C++ class. classA.h class A
I've encountered the strange :colorscheme command behavior. I have some unusual symbols generated by
I've encountered some problems with MongoID. I have three models: require 'mongoid' class Configuration
I'm trying to learn python and have encountered some strange behaviour. I am experimenting
I encountered strange behavior of enum type loaded by different class loader. In common
I have encountered a strange error relating to template class in C++. The codes
I encountered some really strange behaviour in Apache. I have a standard let PHP
I have encountered a very weird behavior while using SimpleDateFormat for parsing a string
I have just encountered a strange situation. I have some basic html: <table bgcolor=#FFFFFF
I have encountered a strange behavior of BindingSource. First I set it's DataSource in

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.