Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (5)
    As of 2020-08-08 04:12 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (51 votes). Check out past polls.