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
Perl

Avatar of undefined
Last Comment
Dave Cross

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
noci

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
fabiano petrone

Hi, noci
Really Great Thnaks for your quick great solution! :=)
Take Care,
Fabiano
David Favor

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

Dave Cross

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy