|
| 1 | +=title How to create an Indian Rupee symbol with Perl code |
| 2 | +=timestamp 2013-03-16T10:30:02 |
| 3 | +=indexes binmode, Unicode, UTF8 |
| 4 | +=status show |
| 5 | +=books beginner_book |
| 6 | +=author szabgab |
| 7 | +=index 1 |
| 8 | +=archive 1 |
| 9 | +=feed 1 |
| 10 | +=comments 1 |
| 11 | +=social 1 |
| 12 | + |
| 13 | +=abstract start |
| 14 | + |
| 15 | +Recently I got an e-mail from one of the readers asking how to create a Indian Rupee symbol with Perl. |
| 16 | +I told him to check <b>what is the Unicode character for the Rupee symbol</b> and then to check |
| 17 | +<b>how to print Unicode characters with Perl</b>. |
| 18 | + |
| 19 | +As this might be interesting to more people, let me show it here. |
| 20 | + |
| 21 | +=abstract end |
| 22 | + |
| 23 | +The first search led me to the Wikipedia page describing the <a |
| 24 | +href="http://en.wikipedia.org/wiki/Indian_rupee_sign">Indian Rupee sign</a>. |
| 25 | + |
| 26 | +Apparently there is a <b>Generic Rupee sign</b> U+20A8 <hl>₨</hl> and a specific <b>Indian Rupee sign</b> U+20B9 |
| 27 | +<hl>₹</hl>. |
| 28 | + |
| 29 | +If you'd like to print a Unicode character to the screen (standard output or STDOUT), then you need to |
| 30 | +tell Perl to change the encoding of the STDOUT channel to UTF8. You can do this with the <hl>binmode</hl> |
| 31 | +function. (Check out the <a href="http://perldoc.perl.org/perluniintro.html">Unicode intro</a> for further details.) |
| 32 | + |
| 33 | +For the specific code points you'd use the <hl>\x</hl> sign and then put the Hexadecimal values, in our case |
| 34 | +20A8 and 20B9 respectively in curly braces. |
| 35 | + |
| 36 | +<code lang="perl"> |
| 37 | +use strict; |
| 38 | +use warnings; |
| 39 | +use 5.010; |
| 40 | + |
| 41 | +binmode(STDOUT, ":utf8"); |
| 42 | + |
| 43 | +say "\x{20A8}"; # ₨ |
| 44 | +say "\x{20B9}"; # ₹ |
| 45 | +</code> |
| 46 | + |
| 47 | + |
| 48 | +Of course if you are creating an HTML page then you'd better iclude the HTML entity representing the same character |
| 49 | +whith is an at sign (&) followed by a pound sign (#), followed by the decimal representation of the code point, |
| 50 | +followed by a semi-colon. To convert the Hexadecimal values to decmal values you can do the following in perl: |
| 51 | + |
| 52 | +<code lang="perl"> |
| 53 | +print 0x20A8, "\n"; # 8360 |
| 54 | +print 0x20B9, "\n"; # 8377 |
| 55 | +</code> |
| 56 | + |
| 57 | +So in HTML you would include <hl>&#8360;</hl> and <hl>&#8377;</hl> respectively. |
| 58 | + |
| 59 | +I have used perl 5.14.2 on Linux to test the above. In older versions of Perl the above might not work. |
| 60 | + |
0 commit comments