John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

Consider:
#... use utf8; foo ("string in utf8 format"); no utf8; foo ("string containing 8-bit character set high chars");
How can foo tell whether its caller had utf8 mode or not?

A better way might be to have foo check the encoding persuasion of the parameter. There is no built-in function for that, and I use the taint-like trick to flip it. But how do I test it? —John

Replies are listed 'Best First'.
Re: Caller's utf8 setting?
by princepawn (Parson) on Nov 01, 2002 at 17:10 UTC
    From a practical standpoint why would this happen. From a problem-solver standpoint, try this:
    package Process::Data::UTF8; sub foo { ... } 1; package Process::Data::NonUTF8; sub foo { ... } 1; use utf8; Process::Data::UTF8::foo ("string in utf8 format"); no utf8; Process::Data::NonUTF8::foo ("string containing 8-bit character set hi +gh chars");
Re: Caller's utf8 setting?
by graff (Chancellor) on Nov 02, 2002 at 14:47 UTC
    I understand your concern -- a package method might be called by one process handling utf8 data, or by another that's handling something else. (Should we be forced to make sure the latter is revised to conform? That depends...)

    The Encode man page in the 5.8.0 distribution seems to have an answer, but maybe not an ideal one. Check the sections titled "The UTF-8 Flag" and (within that) "Messing with Perl's Internals"; if you can trust Perl's application of the UTF-8 flag on scalars in your application, then you can test this flag. (But maybe the nature of the test will change in a later release.)

      At last, a UTF-8 flag test function! I've searched CPAN and ActiveState and although the module doesn't say so, I'm supposing it only works on 5.8, since it uses "internal stuff"?

      —John