Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

How to specify Encode:: <subchar>?

by Marcello (Hermit)
on Mar 18, 2008 at 09:21 UTC ( #674750=perlquestion: print w/replies, xml ) Need Help??

Marcello has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

From the Encode documentation:
Handling Malformed Data ... CHECK = Encode::FB_DEFAULT ( == 0) If CHECK is 0, (en|de)code will put a substitution character in place +of a malformed character. When you encode, <subchar> will be used.
I have been looking all over, but how can I specify this <subchar>? The default is '?' but I would like to use a space instead.


Replies are listed 'Best First'.
Re: How to specify Encode:: <subchar>?
by ikegami (Pope) on Mar 18, 2008 at 12:49 UTC
    How about
    use constant FB_SPACE => sub { ' ' }; $bytes = encode($charset, $chars, FB_SPACE);

    Update: Tested:

    use Encode qw( encode FB_DEFAULT ); use HTML::Entities qw( decode_entities ); use constant FB_SPACE => sub { ' ' }; my $chars = decode_entities('abc&hearts;def'); my $charset = 'US-ASCII'; for my $check (FB_DEFAULT, FB_SPACE) { print encode($charset, $chars, $check), "\n"; }
    abc?def abc def
      Thanks ++ikegami,

      Upgraded my Encode version from 2.01 to the latest 2.24 and using this new functionality.

      Regards, Marcel
Re: How to specify Encode:: <subchar>?
by hipowls (Curate) on Mar 18, 2008 at 10:32 UTC

    <subchar> is specified in a Unicode Character Map (UCM) file. They are used by enc2xs (part of Encode) to generate xs files to be compiled into an encoding. The documentation for enc2xs has a quick guide for making your own encoding.

    If all you want to do is change the substitution character then it should be simple. Find the UCM file for the encoding you want to modify, change <subchar> to the value you want and follow the instructions.

    Good luck!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674750]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2020-06-04 05:55 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (30 votes). Check out past polls.