I need to remove eventual trailing directory separators (slash character '/', maybe several of them together) from a file path. The Perl string with the file path was originally read from a UTF-8 text file and is internally flagged as UTF-8. This is only an example, I am doing more string operations like that.

I would have thought that doing a substr() on a UTF-8 string would respect the UTF-8 flag. However, the string I get back is always flagged as "native/raw bytes". Well, at least with the test strings I am using, which admittedly are all ASCII at the moment. In any case, changing the UTF-8/native flag creates problems in my code later on the line.

Is this substr() behaviour expected? I have tested it with the Perl version v5.26.1 that comes with Ubuntu 18.04.4.


In reply to substr on UTF-8 strings by rdiez

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.