2009年3月26日星期四

符号表和typeglob

终于似乎明白符号表该怎么访问了


#!/usr/bin/perl
use strict;
our $hash="symbol table";
my $name = "hash";
my $globref = ${*::}{$name};
print $globref,"\n";
local *entry = *{$globref};
our $entry;
print $entry,"\n";
print ${*{${*::}{$name}}},"\n"

输出为
*main::hash
symbol table
symbol table

perl中符号表用typeglob来存储perl包变量,它本身也是一个hash,一个typeglob的名字可以通过访问这个hash得到
${*::}{$name}正是获得了$hash对应typeglob的名字,然后把它指向另外一个typeglob,两个typeglob可以用来获得同一个包变量.

2009年3月24日星期二

初审结束


似乎我能混个毕业了

2009年3月23日星期一

XPM图像

X PixMap

X Pixmap (XPM) is an ASCII-text-based image format used by the X Window System.

xpm可以直接用文本表示
vim /usr/share/system-config-network/pixmaps/uk.xpm
static char * uk_xpm[] = {
"21 14 5 1",
" c None",
". c #000080",
"+ c #C0C0C0",
"@ c #FF0000",
"# c #FFFFFF",
"#......#@@@#.....@@++",
"@##....#@@@#...@@##++",
".@@##..#@@@#.@@##..++",
"...@@#.#@@@#@##....++",
"########@@@########++",
"@@@@@@@@@@@@@@@@@@@++",
"@@@@@@@@@@@@@@@@@@@++",
"@@@@@@@@@@@@@@@@@@@++",
"########@@@########++",
"....##@#@@@#.#@@...++",
"..##@@.#@@@#..##@@.++",
"##@@...#@@@#....##@++",
"@@.....#@@@#......#++",
"+++++++++++++++++++++"};

而且是直接表示成c中字符数组的形式,比起其他的图片格式,这种表示方法感觉太简洁强大了。
第一行的意思为:图片宽21为像素,高为14像素,5种颜色,每个数据字母代表一个像素
之后5行为5中颜色的定义,把每种颜色map到一个字母上面
之后便是21×14的像素数据

perl中也可以直接用操作xpm数据。下面是一个gtk2的例子

#!/usr/bin/perl
use strict;
use warnings;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;

my @question_xpm = (
'32 32 17 1',
' c None',

'. c #030303',
'+ c #1A1A1A',
'@ c #4A4A4A',
'# c #616161',
'$ c #939393',
'% c #AAAAAA',
'& c #D2D2D2',
'* c #DCDCDC',
'= c #E2E2E2',
'- c #E6E6E6',
'; c #EDEDED',
'> c #343434',
', c #AEAEAE',
'x c #7B7B7B',
') c #C5C5C5',
'! c #F3F3F3',
' ',
' ',
' ',
' )&&&&)), ',
' )=*=--=-=;*& ',
' )*-;;!;;!;;;** ',
' )=;!)x@>+>#)!;!* ',
' &*!;#.>x%%x..>&!!& ',
' =;!@.$!!;;!=+.+;;=& ',
' );!%..;!;--;!$..x!;& ',
' *;;#..&!-&*;!&..>!;& ',
' &=;#..x!=&*;!&..>!;& ',
' )*;*@>&;&)=;!$..@!;& ',
' &*;;;;=&=;!!+..,!;* ',
' &--***=;!*@..#!;= ',
' )&)&=;!$..+x!;=& ',
' %&;!#.@%;!!=& ',
' &&;$.x!!;;-* ',
' &-!#.!!;=&) ',
' )-!@@;;*) ',
' )*;x$;-) ',
' &;!!!;;* ',
' &*!&@@);=& ',
' *=;@..@;=) ',
' *-;@..>!;* ',
' )-;)>>&-*& ',
' )=!!;;=) ',
' &=-;=) ',
' )**) ',
' ',
' ',
' ');

my ($vsize,$hsize) = (51,51);
my $file = '/usr/share/pixmaps/gnomine/mine.svg';
my $window = Gtk2::Window->new();
my $hbox = Gtk2::HBox->new();
my $button = Gtk2::Button->new();
my $button2 = Gtk2::Button->new();
$button->set_size_request($vsize,$hsize);
$button2->set_size_request($vsize-5,$hsize-5);

#my $image = Gtk2::Image->new_from_file($file);
my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file,$vsize-20,$hsize-20,1);
my $image = Gtk2::Image->new_from_pixbuf($pixbuf);
$button->set_image($image);
my $pixbuf2 = Gtk2::Gdk::Pixbuf->new_from_xpm_data(@question_xpm);
my $image2 = Gtk2::Image->new_from_pixbuf($pixbuf2);
$button2->set_image($image2);
$hbox->pack_start($button,0,0,0);
$hbox->pack_start($button2,0,0,0);
$window->add($hbox);

$window->show_all;
Gtk2->main;

ScreenShot
上面那个奇怪的xpm内容就是这个?号

2009年3月18日星期三

perl6看来遥遥无期了

关于perl6实现的项目
pugs是用haskell写的编译器,07年就基本实现了perl6的语言特性,但当前处于停滞状态,唐凤把这个项目扔下好久了。
http://pugs.blogs.com/pugs/

rakudo:基于parrot虚拟机的perl6项目,最有希望成为perl6的正统,以前由于parrot虚拟机开发的不成熟,开发较为缓慢,自08年以来开发愈加活跃。下面是rakudo的开发状态
http://rakudo.org/
测试越来越多,接近18000,通过测试不过不到8000,看这进度一时半会是出不了稳定版的。

parrot是相当于.net,jvm的虚拟机,这个项目最初是为perl6的开发而生成的,但现在野心巨大,他们希望在parrto虚拟机上能运行所有的动态语言,包括perl,python,ruby等。

Parrot于18号释出了1.0版
http://www.parrot.org/news/2009/Parrot-1.0.0
原以为perl社区会有较大震动,但当日perl社区却基本没人关心这样一件事情。原来parrot这个1.0版本没有任何特殊意义,只是一次普通的释出而已,想让人激动都激动不起来。

PerlMonk上也有一篇帖子,ms还是中国人发的,讨论关于perl6/rakudo和parrot,也是参与人尔尔。
http://perlmonks.org/?node_id=751341

2009年3月15日星期日

有意思的perl单词

perl的文档就像perl里面的关键词一样浩瀚繁多,描述这些文档的关键词也各有特色,简单的列举如下

RFCs:Request For Comments.perl6 Essential这本书里面提到,设计perl6语言是从提交RFCs开始的。任何人都可以提交RFCs,经由perl conference的讨论和修改,才能决定是否接受。
Apocalypses:英语字典里面这样解释apocalypse: a situation in which a lot of people die or suffer, and a lot of damage is done。看样子是天灾人祸的意思,中文解释为天启,但在perl里面意为"revelation(启示)",而不是"end of the world"。perl的apocalypse由larry wall撰写,是对RFCs的系统解释,每一个apocalypse对应Programming Perl的一章。apocalypses也不仅仅是对RFCs的解释,还包括larry wall对各个问题的多种解决途径,以及最后得出的最优方法,同时里面还包括为什么拒绝某些RFCs的提议。
Synopses(概要)是apocalypse的总结
Exegeses(注释)由Domain Conway撰写,是对apocalypses的注释

除了RFCs,所有的文档都可以在这里找到。synopses写了一堆,apocalypses很少,最后的修改时间是2006年3月,而Exegeses更少,最后也是2006年修改。
perl6的RFCs在这里

Format in Perl

下面是一个format的例子
@,^表示替代域,^不同的是可以多行替代
...在域的后部,如果替代域过长则显示...
<,>,|表示左对齐,右对齐和居中显示
##.##表示数字
~~表示重复输出此行
@也占有一个控制位
$~:当前format名字
$^:当前top名字
$=:页面行数
$%:页号

#!/usr/bin/perl
use strict;
use warnings;

my $subject = "The revolution of POE server";
my $name = "woosley";
my $num = 1984.1023;
my $description = "This is a test of the format style in perl, but I
make it have a name The revolution of POE server. added to test";

open FO,">formate.txt";
format FH =
SUBJECT @<<<<<<<<<<<<<<<<<<<<<<<<<<<@####.##
$subject,$num
Assigned to: @<<<<<<<<<<<< ^<<<<<<<<<<<<<<
$name , $description
^<<<<<<<<<<<<<<~~
$description
.
#Below shows how to write an defined format into a opened filehandler
select(FO);
format TOP =
This is page @<<
$%
-------------------
.
select((
select(FO),
$~= 'FH',
$^ = 'TOP',
$= = 5,
)[0]);
write;


以上高亮代码用perltidy生成
perltidy -html -ss >mystyle.css
perltidy -html -pre test.pl
生成的html中会出现tab为8个空格长度而不能对齐的现象,不知道如何解决
发现perltidy不支持中文字符

2009年3月7日星期六

IO::File::MultiLines与创建可安装模块相关

一个小模块
还顺便用它在cpan上面占了个坐,嘿嘿
使用h2xs -XA -n IO::File::MultiLines生成


package IO::File::MultiLines;
use 5.006;
use strict;
use warnings;
use Carp;
use IO::File;
use base qw/IO::File/;


our $VERSION = '0.02';

sub new{ goto &IO::File::new};

sub get_lines{
croak "Can not run under void context" unless defined wantarray;
my ($fh,$n) = @_;
croak <<EOF unless $n=~/^\d+$/;
invalid parameter for get_lines;
EOF
my @tmp;
foreach(1..$n){
my $line = <$fh>;
last unless defined $line;
push @tmp,$line;
}
return wantarray ? @tmp:\@tmp;
}


1;

__END__
=head1 NAME

IO::File::MultiLines

=head1 DESCRIPTION

Simple interface to read multiple lines into array or scalar

=head1 SYNOPSIS

use IO::File::Log;
my $fh = new IO::File::MultiLines "FILENAME","r";

while( my @a = $fh->get_lines(5)){
print @a;
}

=head1 MEHHODS

B<get_lines>

my @array = $fh->get_lines(5);

Load 5 lines of the file content into an array

=head1 SEE ALSO

L<IO::File>

=head1 AUTHOR

woosley.xu E<lt>F<woosley.xu@gmail.com>E<gt>.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 by woosley.xu

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.


=cut


perltidy -html -npod -pre .....

2009年3月5日星期四

几件事

把两个博客合并了,从此perl的帖子也发在这里

前些日子老爸打电话说,外婆中风几年之后前几天去世。我并不感到悲伤,就像大三时候爷爷去世一样,即使我们的感情正如所有祖孙一样的好。我一直认为人老没用了就可以去向马克思报道,不必气若游丝的苟活于世,我认定老了的时候觉得自己没用了我就安眠药送自己上天堂。

今天折腾了一天的WWW::Mechanize,一个错误半天都没有解决,后来发现是fedora编译的rpm不对,用cpan重新装过就好了。发行版包管理的造成混乱实在是一件相当恶心的事情。