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

  • Home
  • SEARCH
  • 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 1107167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:54:00+00:00 2026-05-17T01:54:00+00:00

Okay, this should be really simple, but I have searched all over for the

  • 0

Okay, this should be really simple, but I have searched all over for the answer and also read the following thread:
How do I find the length of a Unicode string in Perl?

It does not help me. I know how to get Perl to treat a string constant as UTF-8 and return the right number of chars (instead of bytes) but somehow it doesn’t work when Perl receives the string via my AJAX call.

Below, I am posting the three Greek letters Alpha, Beta and Omega in unicode. Perl tells me length is 6 (bytes) when it should tell me only 3 (chars). How do I get the correct char count?

#!/usr/bin/perl
use strict;

if ($ENV{CONTENT_LENGTH}) {
    binmode (STDIN, ":utf8");
    read (STDIN, $_, $ENV{CONTENT_LENGTH});
    s{%([a-fA-F0-9]{2})}{ pack ('C', hex ($1)) }eg;
    print "Content-Type: text/html; charset=UTF-8\n\nReceived: $_ (".length ($_)." chars)";
    exit;
}

print "Content-Type: text/html; charset=UTF-8\n\n";
print qq[<html><head><script>
        var oRequest;
        function MakeRequest () {
            oRequest = new XMLHttpRequest();
            oRequest.onreadystatechange = zxResponse;
            oRequest.open ('POST', '/test/unicode.cgi', true);
            oRequest.send (encodeURIComponent (document.oForm.oInput.value));
        }
        function zxResponse () {
            if (oRequest.readyState==4 && oRequest.status==200) {
                alert (oRequest.responseText);
            }
        }
    </script></head><body>
        <form name="oForm" method="POST">
            <input type="text" name="oInput" value="&#x03B1;&#x03B2;&#x03A9;">
            <input type="button" value="Ajax Submit" onClick="MakeRequest();">
        </form>
    </body></html>
];

By the way, the code is intentially simplified (I know how to make a cross-browser AJAX call, etc.) and using the CGI Perl module is not an option.

  • 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-17T01:54:01+00:00Added an answer on May 17, 2026 at 1:54 am

    For a “native” way to accomplish this, you can convert as you copy with this method:

    Set the mode on an in memory file to the mode desired and read from that. This will make the conversion as the characters are read.

    use strict;
    use warnings;
    
    my $utf_str = "αβΩ"; #alpha; bravo; omega
    
    print "$utf_str is ", length $utf_str, " characters\n";
    
    use open ':encoding(utf8)';
    open my $fh, '<', \$utf_str;
    
    my $new_str;
    
    { local $/; $new_str=<$fh>; }
    
    binmode(STDOUT, ":utf8");
    print "$new_str ", length $new_str, " characters"; 
    
    #output:
    αβΩ is 6 characters
    αβΩ 3 characters
    

    If you want to convert the encoding in place, you can use this:

    my $utf_str = "αβΩ";
    print "$utf_str is ", length $utf_str, " characters\n";
    binmode(STDOUT, ":utf8");
    utf8::decode($utf_str);
    print "$utf_str is ", length $utf_str, " characters\n";
    
    #output:
    αβΩ is 6 characters
    αβΩ is 3 characters
    

    You should not shy away from Encode however.

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

Sidebar

Related Questions

Okay, so this should be a simple question, but I'm fairly new at programming,
Okay this may be a simple question but I have yet to come with
Okay, if feel like this should be really simple and accomplished by a function
Okay this might sound simple but there's a catch. I'll try and explain the
Okay this may sound a bit weird but it is what I have to
Okay, I think I might be over-complicating this issue but I truly am stuck.
I think this should be a rather simple thing to do, but I'm completely
This is probably a really easy question to answer, but for some reason I'm
Okay. This is propably very basic and noobie, but I have about 20+ stores
I have heard that I should avoid using 'order by rand()', but I really

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.