存档

‘Perl’ 分类的存档

CPAN中安装模块

2014年5月28日 没有评论

今天在CPAN中安装模块,总是遇到“Can’t exec “make”: No such file or directory at /usr/share/perl/5.18/CPAN/Distribution.pm line 2084“。折腾了半天,才发现是Ubuntu中的make没有装上。按以下方法装好后,就可以在CPAN中正常安装了。

# sudo apt-get update
# sudo apt-get install build-essential
分类: Perl 标签:

搭建第一个Catalyst应用实例

2012年3月22日 没有评论

记录搭建第一个Catalyst应用实例的步骤。前面安装的Catalyst::Devel中已经包括了用来输出HTML的Template Toolkit,现在需要安装的是Catalyst和它的接口Catalyst::View::TT,

# cpan -i Catalyst::View::TT

根据Catalyst::View::TT的标准,在View目录下生成TT.pm文件:

phillip@athens:~/MyApp$ perl script/myapp_create.pl view TT TT
phillip@athens:~/MyApp$ ls lib/MyApp/View/
TT.pm

获取Catalyst::View::TT的版本信息:

$ perl -MCatalyst::View::TT -e 'print Catalyst::View::TT->VERSION'
0.38

修改Makefile.PL文件,在’require’区域后面加上‘Catalyst::View::TT’:

phillip@athens:~/MyApp$ vim Makefile.PL

修改后的Makefile.PL文件:

#!/usr/bin/env perl
# IMPORTANT: if you delete this file your app will not work as
# expected.  You have been warned.
use inc::Module::Install 1.02;
use Module::Install::Catalyst; # Complain loudly if you don't have
                               # Catalyst::Devel installed or haven't said
                               # 'make dist' to create a standalone tarball.

name 'MyApp';
all_from 'lib/MyApp.pm';

requires 'Catalyst::Runtime' => '5.90011';
requires 'Catalyst::Plugin::ConfigLoader';
requires 'Catalyst::Plugin::Static::Simple';
requires 'Catalyst::Action::RenderView';
requires 'Moose';
requires 'namespace::autoclean';
requires 'Config::General';
# This should reflect the config file format you've chosen
requires 'Catalyst::View::TT' => '0.38';
# See Catalyst::Plugin::ConfigLoader for supported formats
test_requires 'Test::More' => '0.88';
catalyst;

install_script glob('script/*.pl');
auto_install;
WriteAll;

在MyApp/root目录中创建输入的HTML模板,将模板文件命名为’phillip.tt’, 其代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">
<html xmlns=" http://www.w3.org/1999/xhtml" xml:lang="zh_ch">
<head>
<title>Phillip的哲学世界</title>
</head>
<body>
<h1>Phillip的哲学世界</h1>
<p>
        哪只球队将获得是今年的意甲冠军? 
        [% word | html %].
</p>
</body>
</html>

现在phillip.tt还不能做任何事,因为没有定义与之关联的应用。下面将生成一个名字为”Phillip”的控制器:

phillip@athens:~/MyApp$ perl script/myapp_create.pl controller Phillip
 exists "/home/phillip/MyApp/script/../lib/MyApp/Controller"
 exists "/home/phillip/MyApp/script/../t"
created "/home/phillip/MyApp/script/../lib/MyApp/Controller/Phillip.pm"
created "/home/phillip/MyApp/script/../t/controller_Phillip.t"

生成在Phillip.pm文件在/lib/MyApp/Controller目录下。

phillip@athens:~/MyApp/lib/MyApp/Controller$ ls
Phillip.pm  Root.pm

修改Phillip.pm,使用前面的phillip.tt模板:

package MyApp::Controller::Phillip;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME
MyApp::Controller::Phillip - Catalyst Controller

=head1 DESCRIPTION
Catalyst Controller.

=head1 METHODS
=cut
=head2 index
=cut

sub phillip : Global {
        my ( $self, $c, @args ) = @_;
        my $word = $args[0] || 'Barnet';
        $c->stash->{template} = 'phillip.tt';
        $c->stash->{word}= $word;
}

=head1 AUTHOR
phillip,,,

=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
__PACKAGE__->meta->make_immutable;
1;

可以使用myapp_test测试一下:

$ perl script/myapp_test.pl /phillip/milan
[debug] Debug messages enabled
[debug] Statistics enabled
[debug] Loaded plugins:
.----------------------------------------------------------------------------.
| Catalyst::Plugin::ConfigLoader  0.30                                       |
'----------------------------------------------------------------------------'

[debug] Loaded dispatcher "Catalyst::Dispatcher"
[debug] Loaded engine "Catalyst::Engine"
[debug] Found home "/home/phillip/MyApp"
[debug] Loaded Config "/home/phillip/MyApp/myapp.conf"
[debug] Loaded components:
.-----------------------------------------------------------------+----------.
| Class                                                           | Type     |
+-----------------------------------------------------------------+----------+
| MyApp::Controller::Phillip                                      | instance |
| MyApp::Controller::Root                                         | instance |
| MyApp::View::TT                                                 | instance |
'-----------------------------------------------------------------+----------'

[debug] Loaded Private actions:
.----------------------+--------------------------------------+--------------.
| Private              | Class                                | Method       |
+----------------------+--------------------------------------+--------------+
| /default             | MyApp::Controller::Root              | default      |
| /end                 | MyApp::Controller::Root              | end          |
| /index               | MyApp::Controller::Root              | index        |
| /phillip/phillip     | MyApp::Controller::Phillip           | phillip      |
'----------------------+--------------------------------------+--------------'

[debug] Loaded Path actions:
.-------------------------------------+--------------------------------------.
| Path                                | Private                              |
+-------------------------------------+--------------------------------------+
| /                                   | /index                               |
| /...                                | /default                             |
| /phillip/...                        | /phillip/phillip                     |
'-------------------------------------+--------------------------------------'

[info] MyApp powered by Catalyst 5.90011
[info] *** Request 1 (1.000/s) [13033] [Thu Mar 22 17:06:34 2012] ***
[debug] Path is "phillip"
[debug] Arguments are "milan"
[debug] "GET" request for "phillip/milan" from "127.0.0.1"
[debug] Rendering template "phillip.tt"
[debug] Response Code: 200; Content-Type: text/html; 
charset=utf-8; Content-Length: 326
[info] Request took 0.052756s (18.955/s)
.------------------------------------------------------------+-----------.
| Action                                                     | Time      |
+------------------------------------------------------------+-----------+
| /phillip/phillip                                           | 0.000292s |
| /end                                                       | 0.046260s |
|  -> MyApp::View::TT->process                               | 0.044756s |
'------------------------------------------------------------+-----------'

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN">
<html xmlns=" http://www.w3.org/1999/xhtml" xml:lang="zh_ch">
<head>
<title>Phillip的哲学世界</title>
</head>
<body>
<h1>Phillip的哲学世界</h1>
<p>
	哪只球队将获得今年的意甲冠军?
	milan.
</p>
</body>
</html>

注意,此时如果在浏览器中访问http://catalyst_ip_or_name:3000/phillip/milan将报错’Page not found’。重新启动Catalyst开发web服务’myapp_server.pl’仍然出现同样的情况。必须要进行编译和安装模块:

phillip@athens:~/MyApp$ perl Makefile.PL
include /home/phillip/MyApp/inc/Module/Install.pm
include inc/Module/Install/Metadata.pm
include inc/Module/Install/Base.pm
include inc/Module/Install/Makefile.pm
Cannot determine perl version info from lib/MyApp.pm
include inc/Module/Install/Catalyst.pm
include inc/Module/Install/Include.pm
include inc/File/Copy/Recursive.pm
*** Module::Install::Catalyst
Please run "make catalyst_par" to create the PAR package!
*** Module::Install::Catalyst finished.
include inc/Module/Install/Scripts.pm
include inc/Module/Install/AutoInstall.pm
include inc/Module/AutoInstall.pm
*** Module::AutoInstall version 1.06
*** Checking for Perl dependencies...
[Core Features]
- Test::More                       ...loaded. (0.98 >= 0.88)
- Catalyst::Runtime                ...loaded. (5.90011 >= 5.90011)
- Catalyst::Plugin::ConfigLoader   ...loaded. (0.30)
- Catalyst::Plugin::Static::Simple ...loaded. (0.29)
- Catalyst::Action::RenderView     ...loaded. (0.16)
- Moose                            ...loaded. (2.0402)
- namespace::autoclean             ...loaded. (0.13)
- Config::General                  ...loaded. (2.50)
- Catalyst::View::TT               ...loaded. (0.38 >= 0.38)
*** Module::AutoInstall configuration finished.
include inc/Module/Install/WriteAll.pm
include inc/Module/Install/Win32.pm
include inc/Module/Install/Can.pm
include inc/Module/Install/Fetch.pm
Writing Makefile for MyApp
Writing MYMETA.yml and MYMETA.json
Writing META.yml
phillip@athens:~/MyApp$ make && make test

注意,make install需要root权限,否则报错:

phillip@athens:~/MyApp$ make install
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ERROR: Can't create '/usr/local/bin'
Do not have write permissions on '/usr/local/bin'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 at -e line 1
make: *** [pure_site_install] 错误 13

使用sudo安装模块:

phillip@athens:~/MyApp$ sudo make install
[sudo] password for phillip: 
Installing /usr/local/share/perl/5.12.4/MyApp/Controller/Phillip.pm
Installing /usr/local/share/perl/5.12.4/MyApp/root/phillip.tt
Installing /usr/local/man/man3/MyApp::Controller::Phillip.3pm
Appending installation info to /usr/lib/perl/5.12/perllocal.pod

现在,在浏览器中可正常访问http://catalyst_ip_or_name:3000/phillip/milan。如果更新了/home/phillip/MyApp/root/phillip.tt模板,变化的内容即使重新启动Catalyst web服务器也无法显示。修改后可以重新编译,或者直接对’usr/local/share/perl/5.12.4/MyApp/root/phillip.tt’进行修改也可以。

还有一点需要注意的,Catalyst web服务器启动最好使用:

$ perl script/myapp_server.pl -r -d

‘-r’参数将使服务在恰当的时候自动重启,比如当发现Phillip.pm时:

Saw changes to the following files:
 - /usr/local/share/perl/5.12.4/MyApp/Controller/Phillip.pm (create)
Attempting to restart the server
分类: Perl 标签:

安装Catalyst

2012年3月20日 没有评论

下午在Ubuntu上装了Catalyst Web开发框架。从此处下载安装脚本,然后用root执行:

#perl cat-install

如果此时直接运行catalyst.pl,回提示安装Catalyst::Devel。

# catalyst.pl MyApp
To use the Catalyst development tools including catalyst.pl and the
generated script/myapp_create.pl you need Catalyst::Helper, which is
part of the Catalyst-Devel distribution. Please install this via a
vendor package or by running one of -
  perl -MCPAN -e 'install Catalyst::Devel'
  perl -MCPANPLUS -e 'install Catalyst::Devel'
BEGIN failed--compilation aborted at /usr/local/bin/catalyst.pl line 24.

安装Catalyst::Devel。

# perl -MCPAN -e 'install Catalyst::Devel'

使用phillip创建一个具体的实例:

phillip@athens:~$ catalyst.pl MyApp
created "MyApp"
created "MyApp/script"
created "MyApp/lib"
created "MyApp/root"
created "MyApp/root/static"
created "MyApp/root/static/images"
created "MyApp/t"
created "MyApp/lib/MyApp"
created "MyApp/lib/MyApp/Model"
created "MyApp/lib/MyApp/View"
created "MyApp/lib/MyApp/Controller"
created "MyApp/myapp.conf"
created "MyApp/myapp.psgi"
created "MyApp/lib/MyApp.pm"
created "MyApp/lib/MyApp/Controller/Root.pm"
created "MyApp/README"
created "MyApp/Changes"
created "MyApp/t/01app.t"
created "MyApp/t/02pod.t"
created "MyApp/t/03podcoverage.t"
created "MyApp/root/static/images/catalyst_logo.png"
created "MyApp/root/static/images/btn_120x50_built.png"
created "MyApp/root/static/images/btn_120x50_built_shadow.png"
created "MyApp/root/static/images/btn_120x50_powered.png"
created "MyApp/root/static/images/btn_120x50_powered_shadow.png"
created "MyApp/root/static/images/btn_88x31_built.png"
created "MyApp/root/static/images/btn_88x31_built_shadow.png"
created "MyApp/root/static/images/btn_88x31_powered.png"
created "MyApp/root/static/images/btn_88x31_powered_shadow.png"
created "MyApp/root/favicon.ico"
created "MyApp/Makefile.PL"
created "MyApp/script/myapp_cgi.pl"
created "MyApp/script/myapp_fastcgi.pl"
created "MyApp/script/myapp_server.pl"
created "MyApp/script/myapp_test.pl"
created "MyApp/script/myapp_create.pl"
Change to application directory and Run "perl Makefile.PL" to 
make sure your install is complete

按照提示运行‘perl Makefile.PL’完成安装:

phillip@athens:~$ cd MyApp/
phillip@athens:~/MyApp$ perl Makefile.PL 

启动Catalyst的Web服务,便于开发调试:

phillip@athens:~/MyApp$ cd script/
phillip@athens:~/MyApp/script$ perl myapp_server.pl 

在浏览器中打开http://catalyst_web_server_ip_or_name:3000可以显示页面。

分类: Perl 标签:

Perl升级后的问题

2012年3月15日 没有评论

在装好Ubuntu 11.10的机器上配置vnc4server,然后想用本地另外一台机器(Ubuntu)远程控制,在这台机器上安装vnc4server时发现了一个报错:

“debconf: Perl may be unconfigured (Can’t locate Debconf/Log.pm in @INC (@INC contains: /usr/local/lib/perl5/site_perl/5.15.8/i686-linux /usr/local/lib/perl5/site_perl/5.15.8 /usr/local/lib/perl5/5.15.8/i686-linux /usr/local/lib/perl5/5.15.8 .) at (eval 1) line 4.”

提示在@INC中无法找到指定的模块。以前没有发现过这个问题,估计是Perl升级到5.15后的原因。@INC在编译时可以设置,可是我没有找到能一次性设定@INC然后使之永久生效的方法。find找到需要的模块在/usr/share/perl5下,然后把它们拷贝到@INC路径目录下,安装就能顺利完成了。现在可以通过vncviewer远程访问和控制ubuntu 11.10了。

分类: Perl 标签: , ,

使用Unicode::UCD获取Unicode字符信息

2012年3月13日 没有评论

Unicode::UCD可以获取Unicode字符信息。Perl 5.12以上版本包含了此模块,而5.10只有升级到更高的版本才能使用该模块。下面是获取汉字“淮”Unicode信息的例子:

#!/usr/bin/perl
use strict;
use warnings;
use Unicode::UCD 'charinfo';
use charnames ':full';
use Data::Dumper;

my $charinfo = charinfo(ord( "\N{U+6dee}" ));

print Dumper $charinfo;
print "显示汉字 ";
binmode STDOUT, ":utf8";
print "\N{U+6dee}\n";

打印的结果:

$VAR1 = {
  'digit' => '',
  'bidi' => 'L',
  'category' => 'Lo',
  'code' => '6DEE',
  'script' => 'Han',
  'combining' => 0,
  'upper' => '',
  'name' => 'CJK UNIFIED IDEOGRAPH-6DEE',
  'unicode10' => '',
  'decomposition' => '',
  'comment' => '',
  'mirrored' => 'N',
  'lower' => '',
  'numeric' => '',
  'decimal' => '',
  'title' => '',
  'block' => 'CJK Unified Ideographs'
  };
显示汉字 淮

汉字的Unicode字符集可以在此处获取。一些Unicode图形(表情)可以参考这里。注意,如果要在系统中显示该表情图形或文字,必须要确定该表情图形和文字在系统字符映射表中有对应的条目,否则是无法正常显示的。比如笑脸”WHITE SMILING FACE”在字符映射表中有对应的条目,可以打印出来“☺”;而“SMILING CAT FACE WITH OPEN MOUTH”没有对应条目就无法正常显示了。Perl的charnames是访问Unicode集较为方便的接口,“\N“可在运行时编译后面花括号{}中的十六进制数和Unicode名称,比如\N{U+263a}和\N{WHITE SMILING FACE}都可以打印出笑脸。

Perl升级到v5.15.8

2012年3月13日 没有评论

Perl最新的开发版本是今年2月20日放出的5.15.8。参考手册,在Ubuntu上从Perl 5.10升级到这个版本:

1. 下载源码:

# wget http://www.cpan.org/src/5.0/perl-5.15.8.tar.gz
# tar xvf perl-5.15.8.tar.gz

2. 编译

# cd perl-5.15.8
# sh Configure -Dinstallusrbinperl  
# make
# make test
# make install

3.检查

# whereis perl
# perl -v
This is perl 5, version 15, subversion 8 (v5.15.8) built for i686-linux
Copyright 1987-2012, Larry Wall
# /usr/bin/perl -v
This is perl 5, version 15, subversion 8 (v5.15.8) built for i686-linux
Copyright 1987-2012, Larry Wall

-Dinstallusrbinperl使编译后的/usr/bin/perl链接为新版本perl,如果不使用此项,/usr/bin/perl -v 仍然显示为5.10。为了方便起见,最好使用此选项。

顺便升级CPAN:

#perl -MCPAN -e shell
cpan>install Bundle::CPAN
分类: Perl 标签: ,