mattdeans has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to get the Unicode of a character in perl?
by davido (Cardinal) on Sep 02, 2011 at 04:53 UTC | |
Modern Perls (the more modern the better) have mostly native Unicode support. Be sure to use feature qw/unicode_strings/;, and you may need to binmode FH, ':utf8'; your input and output filehandles (STDOUT, for example) so as not to have "wide character" warnings and problems. That said, chr and ord should just work as you wish, even with Unicode. For more elaborate dealings, you may need one of the Encode modules, or Unicode::Collate, Unicode::UCD, Unicode::Normalize, or other modules. There is a lot of information out there in Perl's documentation. perlunicode, perluniintro, perlunifaq, perlunitut to name a few. I'm sure that Programming Perl (4th Edition) (due to market in December) will shed additional light on the topic, especially since tchrist is listed as one of the authors. He seems to be waving the Perl Unicode flag a lot these days, so I imagine he will have a lot to contribute on the subject when the book is released. The book Modern Perl deals with Unicode to some degree, and there's a section on it in the Perl Cookbook as well, although the Cookbook was written before Perl really passed puberty with respect to Unicode. Be glad you're dealing with Perl. Few languages have such a degree of native Unicode support as modern versions of Perl. C++, for example, requires the 3rd party ICU library (or something similar) just to get part of the functionality that ships with Perl 5.14. Dave | [reply] [d/l] [select] |
|
Re: How to get the Unicode of a character in perl?
by CountZero (Bishop) on Sep 02, 2011 at 05:06 UTC | |
Output: ☺ has unicode 263A CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James | [reply] [d/l] [select] |
|
Re: How to get the Unicode of a character in perl?
by moritz (Cardinal) on Sep 02, 2011 at 06:40 UTC | |
There's the number of the codepoint, which you can get from ord. The name of a codepoint can be found with charnames::viacode(ord($character)). There are a lot of Unicode properties, which can be queried with regexes:
| [reply] [d/l] [select] |
|
Re: How to get the Unicode of a character in perl?
by Anonymous Monk on Sep 02, 2011 at 07:10 UTC | |
| [reply] [d/l] |
by tchrist (Pilgrim) on Nov 09, 2011 at 18:24 UTC | |
Just to demonstrate how much nicer to look at, and more convenient all around, that the CPAN Data::Dump module is for actually dumping out data than the woefully standard Data::Dumper module is, here is the same code written to use the other module:
And here is the lovely output:
Isn’t that a lot easier on hand and eye? | [reply] [d/l] [select] |
|
Re: How to get the Unicode of a character in perl?
by BrowserUk (Patriarch) on Sep 02, 2011 at 08:10 UTC | |
You can code an effective equivalent of charCodeAt() so:
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |