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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|