Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I haven't used File::BOM, but I gather it will only work if the input data actually contains a byte-order-mark character (in fact, if it begins with said character); assuming this isn't true of your data, it won't help you much.

To elaborate on the first reply (which is basically correct, if you are in fact only deciding between Latin-1 and utf8), check the section about "Handling Malformed Data" in the Encode man page: basically, use the "decode" function like this:

#!/usr/bin/perl use strict; use Encode; die "Usage: $0 latin1.file > utf8.file" unless ( @ARGV == 1 and -f $ARGV[0] ); open( my $fh, "<", $ARGV[0] ) or die "$ARGV[0]: $!"; { local $/; $_ = <$fh>; close $fh; } my $utf8; eval { $utf8 = decode( "utf8", $_, Encode::FB_CROAK ) }; if ( $@ ) { # input was not utf8 $utf8 = decode( "iso-8859-1", $_, Encode::FB_WARN ); } binmode STDOUT, ":utf8"; print $utf8;

If you have to decide between utf8, Latin1, Latin2, Cyrillic, Greek, etc, then you have a harder job: to the extent that the non-Latin1 encodings use the same 8-bit range as Latin-1, Encode will happily pretend that they are all Latin-1, thereby converting them all to the wrong set of utf8 characters.

Encode::guess probably won't help you in that case -- you need to train up some bigram character models for each language... (well, maybe the Lingua branch on CPAN has something to handle this by now).


In reply to Re: how to check the encoding of a file by graff
in thread how to check the encoding of a file by jbrugger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found