How to append BOM at the beginning of a binary
"Nagy Tamas (TVI-GmbH)" <[email protected]> Mon, 8 Jun 2015 15:48:27 +0000
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Hi,
perl -p -i -e 'BEGIN { printf "%c%c%c", 0xEF, 0xBB, 0xBF }' default.xml
perl -p -i -e 'BEGIN { print pack "c3", 0xEF, 0xBB, 0xBF }' default.xml
Both give the same error:
Can't find string terminator "'" anywhere before EOF at -e line 1.
Is it because of Windows CMD? How to get it work?
Tamas Nagy
On 6/8/15 3:58 AM, Nagy Tamas (TVI-GmbH) wrote:
> Hi,
>
> "perl -p -i -e 'BEGIN {print %x "EFBBBF" }' default.xml Can't find
> string terminator "'" anywhere before EOF at -e line 1."
> Can't find string terminator "'" anywhere before EOF at -e line 1.
>
> How to do it with this PERL line?
>
> %x prints hexa, but it does not work.
>
1. The "%x" conversion takes an integer and outputs characters
representing hexadecimal. That's almost the opposite of what
would be required to output the three bytes of a UTF-8 BOM.
2. A format string, which includes the "%x" conversion, is
supported by printf, not print.
Here are two ways that you could write the above:
perl -p -i -e 'BEGIN { printf "%c%c%c", 0xEF, 0xBB, 0xBF }' default.xml
perl -p -i -e 'BEGIN { print pack "c3", 0xEF, 0xBB, 0xBF }' default.xml
3. None of this is necessary. Most applications assume XML files
use UTF-8 encoding, without a BOM. The use of a BOM for UTF-8
is NOT recommended by the Unicode standard and could cause
problems downstream.
4. Placing the UTF-8 BOM at the beginning of a file does not
guarantee that the content of the file is actually encoded as
UTF-8. It just tells downstream applications to decode it
assuming that it is UTF-8. For this reason, the use of a BOM
is particularly unwise for XML, because it would override the
encoding specified in the XML declaration.
5. None of this will solve your original problem, which had to do
with CRLF (0D,0A) pairs embedded in the value strings in the XML.
That problem has nothing to do with whether you use UTF encoding
or not, nor whether your file has a BOM at the beginning or not.
--
David Thomas
Sugar Land, Texas
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs