Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-21 14:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found