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

asked on

little script modification

I use ActivePerl on WIN7.
I've found a useful script for converting ANSI to UTF8 (call it

use File::Slurp;
use Encode;


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:

Open in new window

I've try to use:
open (OUT, <STDOUT>);

Open in new window

but without luck.
Where's my error?
Avatar of noci

Link to home
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


Hi, noci
Really Great Thnaks for your quick great solution! :=)
Take Care,
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,


use strict;
use warnings;

binmode($out, ':utf8');

print while <>;

Open in new window

This reads from STDIN and writes to STDOUT.