We help IT Professionals succeed at work.

little script modification

fabiano petrone
on
128 Views
Last Modified: 2019-03-04
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
Comment
Watch Question

Software Engineer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi, noci
Really Great Thnaks for your quick great solution! :=)
Take Care,
Fabiano
David FavorFractional CTO
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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 CrossPerl programmer, author and trainer
CERTIFIED EXPERT

Commented:
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.