It turns out that most of the confusion was due to File::Slurp RT#84918, submitted by our friend corion. "File-Slurp: read_file() ignores binmode option for short files". If only I had suspected File::Slurp earlier, I could have saved myself (and others) some time.
Here's a complete working example. Note, you must binmode the filehandle with ":encoding(rot13)", not the more terse ":rot13" (which simply won't work). Also, there's no need to explicitly call define_encoding from Encode within the calling package; the line __PACKAGE__->Define('rot13'); does that for us.
package Encode::ROT13; use strict; use warnings; use parent qw( Encode::Encoding ); sub encode($$;$){ my( $obj, $str, $chk ) = @_; $str =~ tr/A-Za-z/N-ZA-Mn-za-m/; $_[1] = '' if $chk; # $_[1] is aliased through the call. Inplace edi +t. # (Remove whole string unless there's an error.) return $str; } no warnings 'once'; *decode = \&encode; # Because rot13( rot13() ) is a round-trip. __PACKAGE__->Define( 'rot13' ); 1; package main; use strict; use warnings; binmode \*DATA, ':encoding(rot13)'; chomp( my @words = <DATA> ); print "$_\n" for @words; __DATA__ Apple cat dog strawberry watermelon
...and the output...
Nccyr png qbt fgenjoreel jngrezryba
...now on to learn how to use the enc2xs tool.
Dave
In reply to Re: Creating (and using) a custom encoding. (SOLUTION)
by davido
in thread Creating (and using) a custom encoding.
by davido
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |