This will encode a complete domain name into Punycode (see RFC 3490).

Update: Corrected small mistake, 7bit ends at \x7F not \x8F...

use IDNA::Punycode; use Net::IDN::Nameprep; use warnings; use strict; our $ACE = 'xn--'; sub punycode { my $utf8name = shift; my @nameparts = split /[\x{002E}\x{3002}\x{FF0E}\x{FF61}]/, $utf8n +ame; my $asciiname = ''; foreach my $part (@nameparts) { if ($part =~ /[^\x00-\x7F]/) { eval { $part = nameprep($part); }; if (my $e = $@) { die "Preparing domain part '$part' of domain '$utf8nam +e' failed with '$e'"; } } if ($part =~ /([\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F] +)/) { die "Illegal character $1 in domain part '$part' of domain + '$utf8name' found"; } if ($part =~ /^\x2D/ or $part =~ /\x2D$/) { die "Domain part '$part' of domain '$utf8name' may not sta +rt or end with a hyphen"; } if ($part =~ /[^\x00-\x7F]/) { if (substr($part, 0, length($ACE)) eq $ACE) { die "International domain part '$part' of domain '$utf +8name' may not start with ACE"; } eval { $part = encode_punycode($part); }; if (my $e = $@) { die "Converting domain part '$part' of domain '$utf8na +me' failed with '$e'"; } $part = $ACE . $part; } if (length($part) < 1 or length($part) > 63) { die "Domain part '$part' of domain '$utf8name' must be bet +ween 1 and 63 characters long"; } } $asciiname = join "\x{002E}", @nameparts; return $asciiname; }

In reply to Punycode by Beechbone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.