Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

creating empty utf8 file

by Anonymous Monk
on Apr 12, 2006 at 09:34 UTC ( [id://542784]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hai monks,
I tried with File::BOM , inorder to create the utf8 file with BOM is automatically written in that file.
The BOM is written as part of the first print command on the handle, so if you don't print anything to the handle, you won't get a BOM.
But i want to create an empty utf8 file with BOM ,
Is there any way to create the BOM other than doing it manually
use File::BOM; open RET,'>:via(File::BOM)',$filename ; #print RET "print BOM"; close RET;
How to create empty file with BOM??

Replies are listed 'Best First'.
Re: creating empty utf8 file
by GrandFather (Saint) on Apr 12, 2006 at 09:45 UTC

    If you can use a different module then:

    use strict; use warnings; use UTF8BOM; my $filename = 'delme.txt'; open outFile, '>', $filename; close outFile; UTF8BOM->insert_into_file ($filename);

    does the trick. See UTF8BOM.


    DWIM is Perl's answer to Gödel
Re: creating empty utf8 file
by borisz (Canon) on Apr 12, 2006 at 11:23 UTC
    open my $fh, '>', 'filename' or die $!; print $fh "\xef\xbb\xbf"; close $fh;
    Boris
Re: creating empty utf8 file
by bart (Canon) on Apr 12, 2006 at 12:27 UTC
    The BOM is written as part of the first print command on the handle, so if you don't print anything to the handle, you won't get a BOM.
    What happens if you print an empty string?
    print RET "";
Re: creating empty utf8 file
by moklevat (Priest) on Apr 12, 2006 at 14:23 UTC
    Beause UTF8 is byte order independent I'm not certain why you want to include a BOM (unless it is simply to have as nearly empty a file as possible with the proper encoding). Having said that, the following will create a file that contains only the UTF8 representation of the byte order mark (\xEF\xBB\xBF). I threw in a newline for good measure, which you can exclude as necessary for your application.

    #!/usr/bin/perl use warnings; use strict; open OUTFILE, ">:utf8", "./myutf8file" or die $!; print OUTFILE "\xEF\xBB\xBF\n"; close OUTFILE;

    Update:I somehow missed borisz's reply above, which does the same thing. I wish I could claim simultaneous posting, but it looks like his was about 3 hours before mine. :-p

Re: creating empty utf8 file
by Anonymous Monk on Apr 12, 2006 at 09:50 UTC
    Thanks GrandFather,
    do we need to use utf8BOM.
    No other way to do that?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://542784]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-04-24 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found