Link to home
Start Free TrialLog in
Avatar of fabiano petrone
fabiano petroneFlag for Italy

asked on

little script modification

Hello,
I use ActivePerl on WIN7.
I've found a useful script for converting ANSI to UTF8 (call it utf8.pl):

use File::Slurp;
use Encode;

$File_Name="c:\\dottorandi.xml";
$file_con=read_file($File_Name);

open (OUT, ">c:\\dottorandiUTF8.xml");
print OUT encode('utf8', $file_con);
close OUT;

Open in new window


but I'd like to use NOT fixed paths to the file to convert (like c:\\dottorandi.xml) but the STDIN and STDOUT, so I can call the script from the command line like:
perl utf8.pl<fileANSI.xml>fileUTF8.xml

Open in new window

I've try to use:
$File_Name=<STDIN>;
...
open (OUT, <STDOUT>);

Open in new window

but without luck.
Where's my error?
Thanks,
Fabiano
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fabiano petrone

ASKER

Hi, noci
Really Great Thnaks for your quick great solution! :=)
Take Care,
Fabiano
For small files noci's approach will work.

If you have large files or require doing some sort of inline transform on records, use something like this...

Note: I'm just going to type this off the top of my head, so there may be syntax errors to fix.

#!/usr/bin/env perl

use strict;
use warnings;
use Carp qw/confess/;

my $ofile = "out-file-name";

my $ifile = shift(@ARGV) || "-";

open(my $in,$ifile) || confess "Unable to open($ifile): $!";
open(my $out,">$ofile") || confess "Unable to open($ofile): $!";

binmode($out, ":utf8");

while (my $rec = <$in>) {
   # Transform(s) occur here...
   print $out $rec;
}

Open in new window

No need to slurp all of the file in one go. Process it a line at a time,

#!/usr/bin/perl

use strict;
use warnings;

binmode($out, ':utf8');

print while <>;

Open in new window


This reads from STDIN and writes to STDOUT.