Use of encoding/decoding and 3-param open
[email protected] ("Greger Leijonhufvud") Tue, 13 Nov 2007 15:39:07 -0000
| Newsgroups | perl.unicode |
|---|---|
| Message-ID | <BB8D9E4E1CDEED41859F33DFF4D8BAAF1898C6@ibgalex02.intecbilling.com> |
I need to take utf8 strings (from an Oracle database) and write them to a file in a single-byte codeset (8859-1, 8859-2....).
I cannot get it to work at all. Test program enclosed
Here is what I get on output:
vzb601b: /vzb/vzb601b/work/gleijonh/utftest $ perl tryit.pl
Name is \x{c384}stan\x{c3a5} Bruk AB\n UTF String:Name is ì
stanì¥ Bruk AB
Name is \N{LATIN CAPITAL LETTER A WITH DIAERESIS}stan\N{LATIN SMALL LETTER A WITH RING ABOVE} Bruk AB Named String: Name is Ã
stanå Bruk AB
Open 'open (OUT1, ">", "sample1")' returns 1
Open 'open (OUT2, ">:utf8", "sample2")' returns 1
Open 'open (OUT3, '>:encoding(iso-8859-1)', 'sample3')' returns undef
Name is ?stan? Bruk AB
(real name in string is 'Östanå Bruk AB' (Swedish...)
What am I doing wrong?
Here is the perl -V
vzb601b: /vzb/vzb601b/work/gleijonh/utftest $ perl -V tryit.pl
Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
Platform:
osname=solaris, osvers=2.9, archname=sun4-solaris-64
uname='sunos magpie 5.9 generic_122300-03 sun4u sparc sunw,sun-fire-v490 '
config_args='-de -Dprefix=/sv/app/perl/product/5.8.8_64 -Dcc=cc -Accflags=-fast -xarch=v9 -DPERL_USE_SAFE_PUTENV -Aldflags=-xarch=v9 -Alddlflags=-xarch=v9 -Aprepend:libswanted=cl pthread -Duse64bitall'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=define use64bitall=define uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-xarch=v9 -fast -xarch=v9 -DPERL_USE_SAFE_PUTENV -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O',
cppflags='-xarch=v9 -fast -xarch=v9 -DPERL_USE_SAFE_PUTENV -I/usr/local/include'
ccversion='Sun C 5.8 2005/10/13', gccversion='', gccosandvers=''
intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=87654321
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='cc', ldflags =' -xarch=v9 -xarch=v9 -L/opt/SUNWspro/prod/lib/v9 -L/lib/sparcv9 -L/usr/lib/sparcv9 -L/usr/ccs/lib/sparcv9 -L/usr/local/lib '
libpth=/opt/SUNWspro/prod/lib/v9 /lib/sparcv9 /usr/lib/sparcv9 /usr/ccs/lib/sparcv9 /usr/local/lib /usr/lib /usr/ccs/lib
libs=-lpthread -lsocket -lnsl -ldl -lm -lc
perllibs=-lpthread -lsocket -lnsl -ldl -lm -lc
libc=/usr/lib/sparcv9/libc.so, so=so, useshrplib=false, libperl=libperl.a
gnulibc_version=''
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
cccdlflags='-KPIC', lddlflags=' -G -xarch=v9 -xarch=v9 -L/opt/SUNWspro/prod/lib/v9 -L/lib/sparcv9 -L/usr/lib/sparcv9 -L/usr/ccs/lib/sparcv9 -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options: PERL_MALLOC_WRAP PERL_USE_SAFE_PUTENV
USE_64_BIT_ALL USE_64_BIT_INT USE_LARGE_FILES
USE_PERLIO
Built under solaris
Compiled at Aug 8 2007 11:16:52
%ENV:
PERL5LIB="/vzb/vzb601b/atai/rel/server/perlmod:/vzb/vzb601b/rel/server/perlmod:/vzb/vzb_app/perl/5.8.8_64/lib/5.8.8:/vzb/vzb_app/perl/5.8.8_64/lib:/vzb/vzb_app/perl/5.8.8_64/lib/site_perl/5.8.8:/vzb/vzb_app/perl/5.8.8_64/lib/site_perl"
PERLDIR="/vzb/vzb_app/perl/5.8.8_64"
PERLIO="utf8"
PERL_ENCODING="utf8"
PERL_HOME="/vzb/vzb_app/perl/5.8.8_64"
PERL_SIGNALS="unsafe"
PERL_UNICODE="DSAL"
@INC:
/vzb/vzb601b/atai/rel/server/perlmod
/vzb/vzb601b/rel/server/perlmod
/vzb/vzb_app/perl/5.8.8_64/lib/5.8.8/sun4-solaris-64
/vzb/vzb_app/perl/5.8.8_64/lib/5.8.8
/vzb/vzb_app/perl/5.8.8_64/lib/5.8.8/sun4-solaris-64
/vzb/vzb_app/perl/5.8.8_64/lib/5.8.8
/vzb/vzb_app/perl/5.8.8_64/lib
/vzb/vzb_app/perl/5.8.8_64/lib/site_perl/5.8.8/sun4-solaris-64
/vzb/vzb_app/perl/5.8.8_64/lib/site_perl/5.8.8
/vzb/vzb_app/perl/5.8.8_64/lib/site_perl/5.8.8/sun4-solaris-64
/vzb/vzb_app/perl/5.8.8_64/lib/site_perl/5.8.8
/vzb/vzb_app/perl/5.8.8_64/lib/site_perl
/sv/app/perl/product/5.8.8_64/lib/5.8.8/sun4-solaris-64
/sv/app/perl/product/5.8.8_64/lib/5.8.8
/sv/app/perl/product/5.8.8_64/lib/site_perl/5.8.8/sun4-solaris-64
/sv/app/perl/product/5.8.8_64/lib/site_perl/5.8.8
/sv/app/perl/product/5.8.8_64/lib/site_perl
Greger Leijonhufvud
Senior Development Consultant
IDA Business Park, Dangan
Galway, Ireland
* +353 (0)91 501378
O2 +44 (0)777 0680026
7 +353 (0)91 527351
@ [email protected] <mailto:[email protected]>
This e-mail and any attachments are confidential and may also be legally
privileged and/or copyright material of Intec Telecom Systems PLC (or its
affiliated companies). If you are not an intended or authorised recipient
of this e-mail or have received it in error, please delete it immediately
and notify the sender by e-mail. In such a case, reading, reproducing,
printing or further dissemination of this e-mail or its contents is strictly
prohibited and may be unlawful.
Intec Telecom Systems PLC does not represent or warrant that an attachment
hereto is free from computer viruses or other defects. The opinions
expressed in this e-mail and any attachments may be those of the author and
are not necessarily those of Intec Telecom Systems PLC.
tryit.pl
(application/octet-stream, 3.4 KB)
#!/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;