Data-Decode
view release on metacpan or search on metacpan
lib/Data/Decode.pm view on Meta::CPAN
Data::Decode - Pluggable Data Decoder
=head1 SYNOPSIS
use Data::Decode;
my $decoder = Data::Decode->new(
strategy => Data::Decode::Encode::Guess->new()
);
my $decoded = $decoder->decode($data);
=head1 DESCRIPTION
WARNING: Alpha grade software.
Data::Decode implements a pluggable "decoder". The main aim is to provide
a uniform interface to decode a given data while allowing the actual
algorithm being used to be changed depending on your needs..
For now this is aimed at decoding miscellaneous text to perl's internal
lib/Data/Decode.pm view on Meta::CPAN
=head2 new
Instantiates a new Data::Decode object.
=over 4
=item strategy
Required. Takes in the object that encapsulates the actual decoding logic.
The object must have a method named "decode", which takes in a reference
to the Data::Decode object and a string to be decoded. An optional third
parameter may be provided to specify any hints that could be used to figure
out what to do.
sub decode {
my ($self, $decoder, $string, $hints) = @_;
# $decoder = Data::Decode object
# $string = a scalar to be decoded
# $hints = a hashref of hints
}
=back
=head2 decode
Decodes a string. Takes in a string, and a hashref of hints to be used
for decoding. The meaning or the usage of the hints may differ between
the actual underlying decoders.
lib/Data/Decode/Encode/HTTP/Response.pm view on Meta::CPAN
sub decode
{
my ($self, $decoder, $string, $hints) = @_;
if (! $hints->{response} || ! eval { $hints->{response}->isa('HTTP::Response') }) {
Data::Decode::Exception::Deferred->throw;
}
my $res = $hints->{response};
my $decoded;
{ # Attempt to decode from header information
my $encoding = pick_encoding(
$res->encoding,
( ($res->header('Content-Type') || '') =~ /charset=([\w\-]+)/g),
);
$decoded = try_decode($encoding, $string);
return $decoded if $decoded;
}
{ # Attempt to decode from meta information
my $p = $self->parser();
my $encoding = pick_encoding(
$p->extract_encodings( $res->content )
);
$decoded = try_decode($encoding, $string);
return $decoded if $decoded;
}
Data::Decode::Exception::Deferred->throw;
}
sub parser
{
my $self = shift;
my $parser = $self->_parser();
if (! $parser) {
lib/Data/Decode/Util.pm view on Meta::CPAN
use strict;
use warnings;
use Encode ();
use Exporter 'import';
our @EXPORT_OK = qw(try_decode pick_encoding);
sub try_decode
{
my ($encoding, $data) = @_;
return () unless $encoding;
my $decoded = eval { Encode::decode($encoding, $data, Encode::FB_CROAK()) };
return $decoded;
}
sub pick_encoding
{
for my $e (@_) {
next unless defined $e;
next unless Encode::find_encoding($e);
return $e;
}
return ();
t/02_chain.t view on Meta::CPAN
isa_ok($decoder, "Data::Decode");
isa_ok($decoder->decoder, "Data::Decode::Chain");
my @encodings = qw(shiftjis 7bit-jis hebrew utf8 euc-jp);
foreach my $encoding (@encodings) {
my $file = File::Spec->catfile("t", "encode", "data", "$encoding.txt");
open(DATAFILE, $file) or die "Could not open file $file: $!";
my $string = do { local $/ = undef; <DATAFILE> };
is($decoder->decode($string), Encode::decode($encoding, $string), "$encoding recognized and decoded properly");
}
( run in 0.299 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )