Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Thursday, October 20, 2005

Unicode conversion in Perl

Starting from Perl 5.6, Perl uses utf-8 for storing character strings internally (Just like Java, which also uses Unicode for storing character strings). To convert a string in native encoding to another encoding, we can think of the same way as what we usually do in Java (if you happens to have some experience in Java character set conversion, you will know what I am saying). e.g. If we want to convert a Big5 string to UTF-16 :


use Encode qw/encode decode/;

$big5str = get_big5_string();
$now_in_utf8 = decode("big5", $big5str);
$now_in_utf16 = encode("UTF-16", $now_in_utf8);



For extra information, if you to want to return an Excel spreadsheet containing Chinese characters from CGI, you can do this:


#!/usr/bin/perl
use Spreadsheet::WriteExcel;
use Encode qw/encode decode/;

my $big5str = get_big5_string();
my $now_in_utf8 = decode("big5", $big5str);
my $now_in_utf16 = encode("UTF-16", $now_in_utf8);

$filename ="excel_file.xls";
print "Content-type: application/vnd.ms-excel\n";
print "Content-Disposition: attachment; filename=$filename\n";
print "\n";

my $workbook = Spreadsheet::WriteExcel->new("-");
my $worksheet = $workbook->addworksheet();

$worksheet->write(1,1,"Non-unicode characters");
$worksheet->write_unicode(1, 2, $now_in_utf16);
$workbook->close();


For M$ Excel before version 2000, you will see square box before each cell, this is due to the BOM character generated during the encode process. Excel 2003 and OpenOffice can recognise this character correctly. To remedy the square bracket, you can just remove it:


my $now_in_utf8 = decode("big5", $big5str);
my $now_in_utf16 = encode("UTF-16", $now_in_utf8);
$now_in_utf16 =~ s/^\x{fe}\x{ff}//;

Tuesday, October 18, 2005

Catalyst Framework

Catalyst is a MVC(Model-View-Controller) Framework in Perl, based on the idea of Maypole and RoR. If you know Perl but want to have a taste of all the juice from RoR, but don't want to learn Ruby, Catalyst should be a good answer.

In particular, it provides URL action mappings, and CRUD application too (i.e. instant web database frontend, once you have your MODEL defined).

Friday, June 03, 2005

php for Perl Programmer

Just recently started to play with PHP (Little bit late, you may think,eh?). There are many similarities between Perl and PHP, but there are many differences also. This article will try to tell the main difference of PHP from Perl. I will keep updating this article while I learn more.

It doesn't mean to be a complete list, and don't ask me why not include this and that. I don't care. :P

1. Flow Control
1.1 If
More 'C'/'Java' like:

if (expression)
statement
elseif (expression)
statement
else
statement

Single statment doesn't need bracket {}.

2. Array
2.1 Hash/Array
Perl hash is now PHP array with arbitrary index. No more '@' symbol.

$array1 = array(
0 => 'value1',
'stringkey' => 'value2',
);
echo $array1[0] . ',' . $array1['stringkey'];


2.2 Append new array element
No need to use 'push'. Just assign it with blank bracket:

$array1[] = 'I will be appended at the end.';

The above only works for simple numeric indexed array. If you use the above type assignment for associative array, PHP will still add to the array, starting with index 0.

If you want to perform similar things for more than one elements, you can use array_push():

array_push($array1, "one value", "two value");


And, if you want to append an array to another array, try array_merge():

$merged_array = array_merge($array1,$array2);

Note that if duplicate keys found in both array, the latter values will overide.

2.3 Loop through array
Do like this:

foreach ($array1 as $value)
echo "value is $value<br>\n";

foreach ($array2 as $key => $value)
echo "key is $key, value is $value<br>\n";