Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: how to output file in Unicode

by philiprbrenan (Monk)
on Aug 23, 2012 at 21:20 UTC ( #989389=note: print w/replies, xml ) Need Help??


in reply to how to output file in Unicode

sub readUnicode($) {my ($f) = @_; open(my $F, "<:encoding(UTF-8)", $f) or die "Cannot open $f for unic +ode input"; local $/ = undef; <$F>; } sub writeUnicode($$) {my ($f, $s) = @_; if ($f =~ /\A(.+[\\\/])/) {my $d = $1; makePath($d); } open(my $F, ">:encoding(UTF-8)", $f) or die "Cannot open $f"; say {$F} $s; }

Replies are listed 'Best First'.
Re^2: how to output file in Unicode
by anakin30 (Acolyte) on Aug 24, 2012 at 08:25 UTC

    apreciate if you could explain below lines and what it does?

    {my ($f, $s) = @_; can you explain what this line does?

    {my $d = $1; can you explain what this line does?

    say {$F} $s; can you explain what this line does?

      sub writeUnicode($$) {my ($f, $s) = @_;

      Assigns parameter 1 to the subroutine from @_ to $f and parameter 2 to $s. One could also write my $f = $_[0]; my $s = $_1;

      if ($f =~ /\A(.+[\\\/])/) {my $d = $1; makePath($d); }

      Extracts the path component of the file name and places it in variable $d so that we can make a directory for the output file. If you know that the output directory exists, then there is no need for this code. The assignment to $d is somewhat verbose, one could use makePath($1) to use the first expression captured by the regular expression in the if statement directly. The regular expression captures the text up to the last \ or / in the file name and uses that as the path component.

      say {$F} $s;

      Writes the contents of $s to the file whose handle is in $F. The file is automatically closed at the end of the block containing my $F. This statement is an alternative to $F->say($s);

        I have written this script below,

        the output file generated but i still seeing, German "Umlaut" in the csv output file. Example : Björn

        why the csv output file is not encoded into encoding(UTF-8)??, am i missed any steps here!!!!

        #!/usr/bin/perl use Net::LDAP; use Encode; $outputfile = 'C:\Meta\MDS\data\out\LicMan1.csv'; $outputfile2 = 'C:\Meta\MDS\data\out\LicMan.csv'; open (INPUT, "<:encoding(UTF-8)", $outputfile) or die "Cannot open fil +ename for input: $!"; while (<INPUT>) { s/"//g; $replace .= $_;} close INPUT or die "Cannot close filename: $!"; open (OUTPUT, ">:encoding(UTF-8)",$outputfile2) or die "Cannot open fi +lename for output: $!"; print OUTPUT $replace; close OUTPUT or die "Cannot close filename: $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989389]
help
Chatterbox?
[cbeckley]: It's nascent, and probably looks like baby Perl to the veterans.
[cbeckley]: I have several questions about it's future development. But it seems like a lot of code to put in a sopw.
[cbeckley]: Should I put the module and example calling code in cufp, and then ask questions in sopw?
[haukex]: You can put it in SOPW, but use <readmore> tags
[haukex]: Also you might want to label it "code review"
[haukex]: Unless you have specific questions, in which case SSCCE's are much better

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2017-03-24 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (304 votes). Check out past polls.