Dear Monks,

I have a latin-1 string that I am trying to do some substitutions on, but the substitution is not happening:

use strict; use warnings; use 5.010; use Encode; my $str = "ThĀt Āpple"; #'A with circumflex' say $str; my $unicode_str = decode('iso-8859-1', $str); my $pattern = "\x{00C2}"; #unicode for 'A with circumflex' $unicode_str =~ s/$pattern//g; my $latin1_str = encode('iso-8859-1', $unicode_str); say $latin1_str;

The output from the first say(), indicates there is something wrong from the very beginning. Instead of seeing an "A with circumflex", I see an "A with tilde". Then no substitution is performed, and I see the same string that is output the first time. My terminal is set to Latin-1.

If I add a use utf8 statement, then for the first say() I see "A with diaeresis(umlaut)", but then the substitutions are performed, and the second say() outputs "Tht pple". However, the utf8 docs specifically warn against using a use utf8 statement for upper Latin-1 codes, and also because I'm not using any utf8 characters in my program file, it doesn't make sense to me to include that statement.

How can I get the first say() to output a string where I see "A with circumflex", and how can I get the substitution to work as well?

Thanks


In reply to encoding question by 7stud

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.