#!/usr/bin/env perl
#---------------------------------------------------------------------------
#---------------------------------------------------------------------------
#       CORE PERL LIBRARIES
#---------------------------------------------------------------------------
use strict;
use FileHandle;
use encoding;
use Encode;
use charnames ':full';
#use open OUT => ":encoding(iso-8859-1)";
use Encode 'decode';

# Force output after every line (keeps stderr and stdout in sync)
autoflush STDOUT 1;
autoflush STDERR 1;

#---------------------------------------------------------------------------
#       PROGRAM LIBRARIES
#---------------------------------------------------------------------------

#---------------------------------------------------------------------------
#       GETOPT VARIABLES
#---------------------------------------------------------------------------


#---------------------------------------------------------------------------
#       PRIVATE GLOBAL CONSTANTS
#---------------------------------------------------------------------------

my $MODULE_NAME = "tryit";

# date formats
my $EFF_DATE_FORMAT        = "DD-MM-YYYY HH24:MI:SS";


#---------------------------------------------------------------------------
#       PRIVATE GLOBAL VARIABLES
#---------------------------------------------------------------------------

my $znow = "";           # Current time string


#---------------------------------------------------------------------------
#       PRIVATE SUBROUTINES
#---------------------------------------------------------------------------

#***************************************************************************
# Processing Subroutines
#***************************************************************************

END {
 
} #end END

#---------------------------------------------------------------------------
#       MAIN CODE
#---------------------------------------------------------------------------

my ($lcsr, $rv, $name);
##******************* START HERE *****************************************


my $utfstring = "Name is \x{c384}stan\x{c3a5} Bruk AB\n";
my $namedstring = "Name is \N{LATIN CAPITAL LETTER A WITH DIAERESIS}stan\N{LATIN SMALL LETTER A WITH RING ABOVE} Bruk AB\n";
#my $textstring  = decode("iso-8859-1",'Östanå Bruk AB\n');
print 'Name is \x{c384}stan\x{c3a5} Bruk AB\n' . "   UTF String:$utfstring";
print 'Name is \N{LATIN CAPITAL LETTER A WITH DIAERESIS}stan\N{LATIN SMALL LETTER A WITH RING ABOVE} Bruk AB' . "  Named String: $namedstring";
#print "Text String: $textstring";

$rv = open (OUT1, ">", "sample1");
if (!$rv) {
    $rv = 'undef';
}
print "Open \'open (OUT1, \">\", \"sample1\")\'  returns $rv\n";
print OUT1 $utfstring;
print OUT1 $namedstring;
#print OUT1 $textstring;
close OUT1;

$rv = open (OUT2, ">:utf8", "sample2");
print "Open \'open (OUT2, \">:utf8\", \"sample2\")\'  returns $rv\n";
print OUT2 $utfstring;
print OUT2 $namedstring;
#print OUT2 $textstring;
close OUT2;

my @list = Encode->encodings();

$rv = open (OUT3, '>:encoding(iso-8859-1)', 'sample3');
if (!$rv) {
    $rv = 'undef';
}
print "Open \'open (OUT3, \'>:encoding(iso-8859-1)\', \'sample3\')\'  returns $rv\n";
print OUT3 $utfstring;
print OUT3 $namedstring;
#print OUT3 $textstring;
close OUT3;

my $utfstring1 = encode('iso-8859-1', $utfstring);
print $utfstring1 . "\n";

##******************* END HERE   ******************************************

exit 0;
