After much study, I determined that what your program does to a file is replace non-ascii characters with their html equivalents. Your hex sequences suggest that the external file is encoded as utf8, but you read it as a string of bytes.

It has already been shown that your approach can be made to work. I prefer the following approach because its purpose is obvious. It lets perl decode the utf8 so it can substitute characters rather than byte strings. Note that all characters are treated the same. It does not matter how many bytes they require. Note also that the hash keys are the actual characters. The code point notation is just a convenient way to specify them without needing utf8 in the Perl code.

use strict; use warnings; use feature 'state'; use feature 'say'; #use utf8; #refer to node id=11154836 below use autodie; use Encode qw(decode); while (my $clmnVal = <>) { $clmnVal = decode('UTF-8', $clmnVal); chomp $clmnVal; $clmnVal = smblRpr( $clmnVal ); #say unpack('U0H*', $clmnVal); say $clmnVal; } exit; sub smblRpr { my $strng = $_[0]; state %smblCo = ( # code point => HTML description "\N{U+201A}" => "&#8218;", # SINGLE LOW-9 QUOTATION MARK "\N{U+201C}" => "&#8220;", # DOUBLE LEFT QUOTATION MARK "\N{U+201D}" => "&#8221;", # DOUBLE RIGHT QUOTATION MARK "\N{U+201E}" => "&#8222;", # DOUBLE LOW-9 QUOTATION MARK "\N{U+2013}" => "&#8211;", # EN DASH "\N{U+2014}" => "&#8212;", # EM DASH "\N{U+2018}" => "&#8216;", # LEFT SINGLE QUOTATION MARK "\N{U+2019}" => "&#8217;", # RIGHT SINGLE QUOTATION MARK "\N{U+2020}" => "&#8224;", # DAGGER "\N{U+2026}" => "&#8230;", # HORIZONTAL ELLIPSIS "\N{U+2122}" => "&#8482;", # TRADE MARK SIGH "\N{U+00A8}" => "&#169;", # COPYRIGHT SIGN "\N{U+00AE}" => "&#174;", # REGESTERED SIGN "\N{U+00B1}" => "&#177;", # Plus-Minus sign "\N{U+00BC}" => "&#188;", # Vulgar fraction one quarter "\N{U+00BD}" => "&#189;", # Vulgar fraction one half "\N{U+00BE}" => "&#190;", # Vulgar fraction three quarter "\N{U+00B0}" => "&#176;", # Degree sign ); state $regex = do{ my @chars = keys %smblCo; local $" = '|'; qr/@chars/; }; return ($strng =~ s/($regex)/$smblCo{$1}/gr); }

UPDATE: Removed use utf8 from code per hippo's comment.

Bill

In reply to Re: Hex regex fails in subroutine by BillKSmith
in thread Hex regex fails in subroutine by Version7

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.