Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^2: Creating (and using) a custom encoding.

by Khen1950fx (Canon)
on May 30, 2013 at 20:29 UTC ( #1036131=note: print w/replies, xml ) Need Help??

in reply to Re: Creating (and using) a custom encoding.
in thread Creating (and using) a custom encoding.

OK. Now that I'm thoroughly confused:-):
package Encode::ROT13; use strict; use warnings; use Encode; use parent qw( Encode::Encoding ); __PACKAGE__->Define( 'rot13' ); no warnings 'redefine'; sub encode($$;$){ my( $obj, $str, $chk ) = @_; $str =~ tr/A-Za-z/N-ZA-Mn-za-m/; $_[1] = '' if $chk; return $str; } *decode = \&encode; 1; package main; use strict; use warnings; use Encode 'define_encoding'; use File::Slurp; my $object = {}; bless($object, 'Encode::ROT13'); define_encoding( $object, 'rot13' ); my(@words) = read_file( \*DATA, chomp => 1, binmode => ':rot13' ); print "$_\n" for @words; my(@list) = Encode->encodings(); print "Checking for rot13: \n"; foreach my $list(@list) { print "\t$list\n";; } __DATA__ Apple cat dog strawberry watermelon

Replies are listed 'Best First'.
Re^3: Creating (and using) a custom encoding.
by davido (Archbishop) on May 30, 2013 at 20:56 UTC

    I'm guessing that doesn't work for you either.

    A few nits (even though neither of us have it working yet): Encode doesn't need to be 'used' in package Encode::ROT13 (and eliminating it eliminates the "redefined" warnings). Also, blessing the object into package Encode::ROT13 may as well be written as a constructor in Encode::ROT13, and then called with the traditional Encode::ROT13->new syntax from the calling package (main) .... not that it matters -- it still doesn't work.

    Also, even if you completely eliminate 'use Encode qw/ define_encoding /; from package main, and eliminate the object construction as well as the call to define_encoding, 'rot13' still shows up in the encodings list. However, placing a "warn" inside of Encode::ROT13::encode shows that the function never gets called.

    What are we missing? :) There really seems to be a disconnect between the POD and reality. But I'm sure it works for someone (who knows how to do it right). Otherwise, the whole Encode::Encoding module is pretty useless.


      Thanks for the clarification.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1036131]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2017-10-22 13:12 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (273 votes). Check out past polls.