Badger::Codec::Encode - codec wrapper around Encode


Badger documentation Contained in the Badger distribution.

Index


Code Index:

NAME

Top

Badger::Codec::Encode - codec wrapper around Encode

SYNOPSIS

Top

    use Badger::Codec::Encode;

    my $codec    = Badger::Codec::Encode->new();
    my $encoded = $codec->encode( utf8 => "...some utf8 data..." );
    my $decoded = $codec->decode( utf8 => $encoded );

DESCRIPTION

Top

This module is a subclass of Badger::Codec implementing a very thin wrapper around the Encode module. It exists only to provide a consistent API with other Badger::Codec modules and to facilitate codec chaining.

You would normally use a codec via the Badger::Codecs module.

    use Badger::Codecs 
        codec => 'encode';

    my $encoding = 'UTF-8';
    my $uncoded  = "...some UTF-8 data...";
    my $encoded  = encode($encoding, $uncoded);
    my $decoded  = decode($encoding, $encoded)

The above example is identical to using the Encode module directly:

    use Encode;     # also exports encode()/decode()

In addition, a Badger::Codec::Encode object will be available via the codec() subroutine.

    my $encoded  = codec->encode($encoding, $uncoded);
    my $decoded  = codec->decode($encoding, $encoded)

METHODS

Top

encode($encoding, $data)

Method for encoding data which forwards all arguments to the Encode encode() method. The first argument is the encoding, the second is the data to encode.

    $encoded = Badger::Codec::Encode->encode( utf8 => $data );

decode($encoding, $data)

Method for decoding data which forwards all arguments to the Encode decode() method. The first argument is the encoding, the second is the data to decode.

    $decoded = Badger::Codec::Encode->decode( utf8 => $encoded );

encoder()

This method returns a reference to the real subroutine that's doing all the encoding work, i.e. the encode() function in Encode.

decoder()

This method returns a reference to the real subroutine that's doing all the encoding work, i.e. the decode() method in Encode.

AUTHOR

Top

Andy Wardley http://wardley.org/

COPYRIGHT

Top

SEE ALSO

Top

Encode, Badger::Codecs, Badger::Codec, Badger::Codec::Unicode.


Badger documentation Contained in the Badger distribution.

#========================================================================
#
# Badger::Codec::Encode
#
# DESCRIPTION
#   A codec wrapper for Encode
#
# AUTHOR
#   Andy Wardley   <abw@wardley.org>
#
#========================================================================

package Badger::Codec::Encode;

use Badger::Class
    version => 0.01,
    base    => 'Badger::Codec';

use Encode qw();
use bytes;

sub encode {
    my $self = shift;
    # No, really, it's OK.  This isn't one of those kind of GOTOs.
    # This is an Offically OK version of GOTO which is really a special
    # kind of subroutine call.  See: perldoc -f goto
    goto &Encode::encode;
}

sub decode {
    my $self = shift;
    # we use goto rather than a call because a) it's quicker (the call 
    # stack frame is re-used) and b) because the prototypes for encode()
    # and decode() would require us to shift all the arguments off the 
    # stack in order to pass them to encode()/decode() in an orderly
    # fashion, e.g. my ($enc, $data) = @_; Encode::encode($enc, $data)
    # rather than: Encode::encode(@_);  # not allowed - prototype mismatch
    goto &Encode::decode;
}

sub encoder {
    \&Encode::encode;
}

sub decoder {
    \&Encode::decode;
}

1;


__END__

# Local Variables:
# mode: Perl
# perl-indent-level: 4
# indent-tabs-mode: nil
# End:
#
# vim: expandtab shiftwidth=4: