Data::Decode::Util - Utilities


Data-Decode documentation Contained in the Data-Decode distribution.

Index


Code Index:

NAME

Top

Data::Decode::Util - Utilities

SYNOPSIS

Top

  use Data::Decode::Util qw(try_decode pick_encoding);

METHODS

Top

try_decode

pick_encoding


Data-Decode documentation Contained in the Data-Decode distribution.

# $Id$
#
# Copyright (c) 2007 Daisuke Maki daisuke@endeworks.jp>
# All rights reserved.

package Data::Decode::Util;
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 ();
}

1;

__END__