CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Display Japanese characters

 



114211
New User

Mar 2, 2010, 8:37 AM

Post #1 of 3 (2385 views)
Display Japanese characters Can't Post

I am a total newb - I just started using perl about two days ago and tried cramming some of the shorthand code but I can't get it. Could someone tell me how I can get Japanese characters to display in this script? Please show me what to say and where to put it because I really don't know what I'm doing.

I got this script from someone else which simply displays all the values of the form.



Code
#!/usr/bin/perl -w 

#=========================================================================
# Copyright 1999-2001 Idocs Guide to HTML, http://www.idocs.com/
# You may distribute this program freely, but keep
# this notice in place.
#=========================================================================

#=========================================================================
# modules
#
use strict;
use CGI;
use CGI::Carp 'fatalsToBrowser', 'croak';

# use lib 'pt/PtLib';
# use lib 'ads/';
# use Banner;
#
# modules
#=========================================================================

#=========================================================================
# variables
#
my (
$query, # CGI query object
$maxFields, # maximum number of fields to accept
$rand, # random number for banners
$inputdata, # filehandle for uploaded files
@inputVals, # values for a field (they can send more than one)
$inputVal, # a single values for a field
$modval, # modified value of
$fileinfo, # uploaded file information
@params, # array of all fields sent
$key,$val, # key an value for $fileinfo hash

$ad,$adcode,
);
$query=CGI->new; # get CGI object
$maxFields=100; # maximum number of fields to accept
$rand = int(rand(100000)); # random number for ad banner

#
# variables
#=========================================================================
#=========================================================================
# top of page
#
print $query->header;
print <<"(TOPOFPAGE)";
<HTML>
<HEAD>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<TITLE>Idocs Guide to HTML: My CGI</TITLE>
</HEAD>
<BODY>

<TABLE WIDTH="100%" CELLSPACING=0 CELLPADDING=0>
<TR VALIGN=TOP>
<TD><A HREF="/" TARGET="_top"><IMG
SRC="../graphics/logo.med.guide.gif"
WIDTH=157 HEIGHT=75 ALT="Idocs Guide to HTML" BORDER=0></A></TD>
<TD ALIGN=RIGHT></TD>
</TR></TABLE>

<H1>My CGI</H1>

This CGI displays the <CODE>name=value</CODE> pairs sent to it. Feel
free to use this CGI for practice making HTML forms. This CGI is provided
compliments of the <A HREF="http://www.htmlcodetutorial.com/">HTML Code Tutorial</A>.<P>

(TOPOFPAGE)
#
# top of page
#=========================================================================

#=========================================================================
# list
#

# get list of fields
@params = $query->param;
if (@params > $maxFields)
{croak "No more than $maxFields fields please"}

# open table if necessary
print "<HR><P><TABLE BORDER CELLPADDING=4>\n" if @params;

PARAMLOOP:
foreach my $paramKey (@params) {
print '<TR VALIGN=TOP>';

#----------------------------------------------------------------
# value
#
$inputdata = $query->param($paramKey);

# print STDERR "\n"; # TESTING
# the problem with "Use of uninitialized value in hash element at (eval 6) line 3."
# happens during the next line

$fileinfo = $query->uploadInfo($inputdata);

# if it's an uploaded file
if (defined $fileinfo) {
print
"<TH>", tablesc($paramKey), "</TH>",
"<TD><TABLE BORDER BGCOLOR=\"#FFFFCC\" CELLPADDING=4>\n";

while (($key,$val) = each %{$fileinfo}) {
# content-disposition gives several pieces of
# information about the file, so let's parse it up
if (lc($key) eq 'content-disposition') {
foreach my $dis (split(m|\s*;\s*|,$val)) {
my @dispieces=split(m/\s*=\s*/,$dis,2);
next unless defined $dispieces[1];
next if $dispieces[0] =~ m|^name$|i;
print
'<TR><TD>',
tablesc($dispieces[0]),
'</TD><TD><CODE>',
tablesc($dispieces[1]),
"</CODE></TD></TR>\n";
}
}

# else just print the field
else {
print
'<TR><TD>',
tablesc($key),
'</TD><TD><CODE>',
tablesc($val),
"</CODE></TD></TR>\n";
}
}

# output the file size
seek($inputdata,0,2); # go to end of file handle
print
'<TR><TD>size</TD><TD>',
tell($inputdata),
"</TD></TR>\n";
print '</TD></TABLE>'; #
}

# else it's not an uploaded file
else {
# get array of input values
@inputVals=$query->param($paramKey);
@inputVals=grep(tablesc($_),@inputVals);

# name of field
print '<TH ROWSPAN=',
scalar(@inputVals), '>',
tablesc($paramKey), '</TH><TD><PRE>';

#out put value(s) for field
foreach $inputVal (@inputVals)
{$inputVal = tablesc($inputVal)}
print
join("</PRE></TD></TR>\n<TR><TD><PRE>", @inputVals),
'</PRE></TD>';
}
#
# value
#----------------------------------------------------------------

print "</TR>\n";
}

# close table if necessary
print "</TABLE><P>\n" if @params;

#
# list
#=========================================================================

#=======================================================================
# bottom of document
#

# ************************************************************************
# *** IF YOU COPY My CGI, ABSOLUTELY PLEASE DO NOT CHANGE THIS SECTION ***
# ************************************************************************

print <<"(END BOTTOM OF PAGE)";
<HR><P>
A lot of people have asked to see the code for this CGI.
OK, <A HREF="http://www.htmlcodetutorial.com/index_famsupp_91.html">here
it is</A>.

</BODY>
</HTML>
(END BOTTOM OF PAGE)
#
# bottom of document
#=======================================================================

#=======================================================================
# tablesc
# return undefined or for space-only/empty string,
# otherwise change < > and & to character entities
#
sub tablesc
{
return " " if (! defined $_[0]) || ($_[0] !~ m|\S|);
$_[0] =~ s|&|&|gso;
$_[0] =~ s|<|<|gso;
$_[0] =~ s|>|>|gso;
$_[0];
}
#
# tablesc
#=======================================================================


The Japanese comes out as gobbily gook, like this:
何駅まで

I saved the form in utf-8. Also, for reasons I'm not sure of, if I try to save a perl file as utf-8 it won't execute. If someone could help me out it would be appreciated


7stud
Enthusiast

Mar 2, 2010, 9:44 AM

Post #2 of 3 (2381 views)
Re: [114211] Display Japanese characters [In reply to] Can't Post


Quote
Please show me what to say and where to put it because I really don't know what I'm doing.

Ah. Ok. You want to hire a programmer. For $200 I'll write the code.


rovf
Veteran

Mar 3, 2010, 4:48 AM

Post #3 of 3 (2371 views)
Re: [114211] Display Japanese characters [In reply to] Can't Post

Seems to be like an encoding problem.

As for Perl scripts written in UTF-8, did you check perldoc utf8? According to the docs, you either have to use this pragma, or ensure that your source file starts with a BOM (i.e. you need an editor which can write the BOM).

Also, I would make sure that you use at least perl 5.8. I don't know whether UTF8 source code was present in the earlier versions.

Now for the display of the characters on your website: Did you ensure that the HTML page really had a correct UTF8 content-type header, AND that the browser would be able to display them correctly? The latter you could test by emitting the japanese character as an XML entity (&hxHHHH;) instead of writing it literally.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives