| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Encode::First; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 793 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 101 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = '0.01'; | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 10 | use Carp (); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 34 |  | 
| 7 | 2 |  |  | 2 |  | 2914 | use Encode (); | 
|  | 2 |  |  |  |  | 36977 |  | 
|  | 2 |  |  |  |  | 627 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | require Exporter; | 
| 10 |  |  |  |  |  |  | *import = \&Exporter::import; | 
| 11 |  |  |  |  |  |  | our @EXPORT = qw( encode_first ); | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub encode_first { | 
| 14 | 8 |  |  | 8 | 1 | 17 | my($encodings, $str) = @_; | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 8 |  |  |  |  | 28 | $encodings = _encodings($encodings); | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 8 |  |  |  |  | 27 | for my $enc (@$encodings) { | 
| 19 | 17 |  |  |  |  | 21 | my $copy  = $str; # Encode::encode might break the original string | 
| 20 | 17 |  |  |  |  | 20 | my $bytes; | 
| 21 | 17 |  |  |  |  | 22 | eval { | 
| 22 | 17 |  |  |  |  | 63 | $bytes = Encode::encode($enc, $str, Encode::FB_CROAK); | 
| 23 |  |  |  |  |  |  | }; | 
| 24 | 17 | 100 |  |  |  | 375 | unless ($@) { | 
| 25 | 8 | 50 |  |  |  | 52 | return wantarray ? ($enc, $bytes) : $enc; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 0 |  |  |  |  | 0 | Carp::croak("No encoding can encode the given string."); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | sub _encodings { | 
| 33 | 8 |  |  | 8 |  | 12 | my $encodings = shift; | 
| 34 | 8 | 50 | 33 |  |  | 47 | return if ref $encodings && ref $encodings eq 'ARRAY'; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 8 | 50 | 33 |  |  | 27 | Carp::croak "Unknown reference type ", ref $encodings | 
| 37 |  |  |  |  |  |  | if ref $encodings && ref $encodings ne 'ARRAY'; | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 8 |  |  |  |  | 58 | return [ split /[:,]/, $encodings ]; | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | 1; | 
| 43 |  |  |  |  |  |  | __END__ |