http://qs1969.pair.com?node_id=11154835


in reply to Hex regex fails in subroutine

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

Replies are listed 'Best First'.
Re^2: Hex regex fails in subroutine
by hippo (Bishop) on Oct 06, 2023 at 08:16 UTC
    The code point notation is just a convenient way to specify them without needing utf8 in the Perl code.

    I understand this but what I don't get is why you've done this after specifying use uft8; which is the explicit declaration that you do intend to have utf8 in the Perl code. This seems contradictory to me - what am I missing here? Thanks.


    🦛

      [Nevermind. I had missed the point.]

Re^2: Hex regex fails in subroutine
by ikegami (Patriarch) on Oct 06, 2023 at 16:13 UTC

    I would also work with the decoded strings. But I wouldn't have a table. If you have to use entities because you can't handle UTF-8, it make no sense to encode a subset only a subset of Unicode using entities. You should encode every non-ASCII Code Point.

    See HTML::Entities's encode_entities_numeric.