Create your blog and photo album with postbit
Create your blog and photo album

Create new post

Content:

Upload a picture:
Tags (keywords separated by comma)

Save Cancel
perl:   Followers: 2 ; Following: 2

Explore perl's photo albums:
Perl images (1)
Photos from posts (2)

Unicode Characters in Perl6


 

Perl 6 uses UTF-8 by default. Here are some references and Perl6 examples using Unicode characters and valid Unicode operators.

Question: How to print the name of an Unicode character?
Anwer: Use the "uniname" operator
say uniname("π"); # GREEK SMALL LETTER PI
or
say "π".uniname;

Question: How can I print an Unicode character using it's numerical code ?
Answer:
960.base(16)

Prints Unicode representation of hexadecimal "263A":
say "\x263a"; # prints Smiling Face ☺
or the same using base 10 number:
say chr(9786); # prints Smiling Face ☺

that's because 263A in Hexa is 9786 in Decimal, see next question.

Question: How can I convert hexa to decimal and decimal to hexa ?
Answer:
say 9786.base(16); # 263A. It will convert the number to base 16
say :16("263A"); # 9786, it's the reverse operation, back to base 10 (from base 16).
 

Question: How can I print a list of all Unicode characters?
Answer: The following script can be used to print the list of all Unicode names and characters:

say "CHARACTER (CODE NUMBER) CODE NAME";

for (80..120) -> $code {
  if ($code < 55296 || $code > 57343) { # Invalid code numbers
    say $code.chr ~ " ($code) " ~ $code.uniname;
  }
}
 

 

 

Code examples:

say "⁂".uniname; # It will print "ASTERISM"

say "\c[SKULL]"; # It will print "💀"
say "π".ord; # 960. ord method returns the codepoint number of the letter (grapheme)

say chr(960); # Prints π

say ("α".."ω").list; # Prints Greek alphabet

say π³; # same as say pi**3

say (1,2,3,4) ∩ (2,4,6,8); # intersection is set(2,4)

 

See the Unicode Table with interesting unicode characters and symbols that can be copied and used in your Perl 6 programs.

 

List of useful Unicode characters in Perl 6:


Perl 6 Unicode Constants - Pi and Tau (π , τ) greek letters:

π = 3.14159265358979: say pi

τ = 6.28318530717959: same as 2*pi or tau

 


Math operators and Superscripts:

¹ = power of 1: **1
² = power of two (Raise to the second power): **2
³ = power of three (Raise to the third power): **3

⁻¹ :Raise to negative first power: **-1
⁻²: Raise to the negative second power: **-2
⁻³: Raise to the negative third power: **-3

 

Sets:

∈ = is an element of: (elem)
∉ = is not an element of: !(elem)
∪ = union: (|)
∩ = intersection: (&)
⊆ = subset: (<=)
⊈ = not a subset: !(<=)

 

More references:

 

Post by perl (2016-01-13 10:23)

Tags: perl6 unicode

Post your comment:

Name: Email: Site:




| Explore users | New posts | Create your blog | Create your photo album |
| About Postbit | Our blog | Terms of use | Contact Postbit |


Copyright © 2017 - postbit.com