存档

‘Perl’ 分类的存档

去掉^M

2023年5月26日 没有评论

去掉符号^M(即”\015″)

分类: Perl 标签:

X11:GUITest

2023年5月10日 没有评论

sudo apt-get install -y libx11-guitest-perl

分类: Perl 标签:

Perl Jifty研究01

2020年2月15日 评论已被关闭

准备写一个基于文本的报价系统,计划用Jifty开发框架。今天晚上完成了开发环境的搭建,在之前装httpd+CGI的CentOS中,做了一些调整。记录如下:
1. CentOS7.2的网络配置在/etc/sysconfig/network-scripts/下的”ifcfg-“开头的系列文件中。只要是以”ifcfg-“开头的,配置都会被系统读出。这个和之前的版本是不一样(即便加上后缀.bak之类也会配置IP)。使用”ip addr list”查看新的IP是否设置成功。
2. CentOS7.2停止开机自动启动服务,使用”systemctl disable httpd-service”取消httpd开机自动重启。“systemctl –type=service”查看所有开机自动运行的服务列表。“systemctl list-units”显示所有当前服务,”systemctl list-unit-files”显示系统所有服务(包括未启动的)。这个和之前的chkconfig配置也不一样。

考RHCE都是十五年前的事情了,这些年对Linux系统的深入研究少了,还得认真补充学习。

使用CPAN安装Jifty, 总是遇到CPAN的网络速度慢或网络无法访问问题。于是想办法把CPAN设置为国内镜像站点后解决。修改CPAN的配置文件MyConfig.pm中urllist对应的链接信息,按照http://www.cpan.org/SITES.html所列出的站点改为国内镜像。 具体步骤如下。

# vim ./cpan/CPAN/MyConfig.pm
'urllist' => [q[http://mirrors.163.com/cpan/], 
              q[ftp://mirrors.ustc.edu.cn/CPAN/], 
              q[http://mirror.lzu.edu.cn/CPAN/]],

然后就可以通过CPAN顺利安装Jifty了。

查看安装的Perl模块有两个方法:

#perldoc -t perllocal | grep Jifty
Fri Feb 14 23:17:01 2020: "Module" Jifty::DBI

第二种方式是使用以下脚本

#!/usr/bin/perl
use strict;
use ExtUtils::Installed;
my $inst = ExtUtils::Installed->new();
my @modules = $inst->modules();
foreach  (@modules) {
        my  $ver = $inst->version($_) || "???";
        printf("%-22s -Version- %-22s\n", $_, $ver);
}
exit;
分类: Perl 标签:

perldoc的使用问题

2019年5月30日 没有评论

Ubuntu中的perldoc无法显示perlfunc,perlsyn文档。原因是这些文档的pod没有安装。解决方法是先用perl -v查询到per的版本号,然后下载对应的源代码。将源代码中的Pod目录下的所有.pod文件,复制到perl -V列出的任何一个目录中。即可使用

# perldoc perlfunc
# perldoc perlsyn
分类: Perl 标签:

Perl通过CGI操作Excel文档

2019年5月21日 没有评论

着手写一个工具: 在Web页面中选择器件类型和数量,然后生成配置报价预览,并提供销售配置表和BOM表的xls下载链接。

1. 搭建开发平台:
安装CentOS7.2最小版,配置网络, 注意网卡配置文件中的网关字符串是NETMASK,不是MASK。

yum install vim
yum install gcc
yum install httpd

配置httpd, 以/var/www/cgi-bin/为默认目录,该目录中index.html为默认主页;
编辑/var/www/cgi-bin/目录下的.htaccess文件:

# 使用rewrite方法,实现http://172.16.210.143/test.pl 
# 为 http://172.16.210.143/test (不带perl的后缀)
RewriteEngine on
RewriteRule ^test$ test.pl
RewriteRule ^result$ result.pl
# 实现xls文件的自动下载
<Files *.xls>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>

2. 编辑index.html页面,获取输入信息。index.html的forum中输入的信息传递到result.pl。

3. Perl无法实现在同一个xls表格中单元格(cell)的复制(内容和格式),必须先生成一个拷贝xls, 然后对拷贝的xls进行内容填充和格式编辑。如果通过CPAN安装Spreadsheet::ParserExcel和Spreadsheet::WriteExcel模块,可以安装成功,也能在命令行模式正常运行,但是如果在浏览器中运行result.pl时,会提示报错

[cgi:error] [pid 4778] [client 192.168.1.104:52082] 
AH01215: Can't loc +ate Spreadsheet/ParseExcel.pm in 
@INC (@INC contains: /usr/local/lib6 +4/perl5 /usr/local/share/perl5 
/usr/lib64/perl5/vendor_perl /usr/shar +e/perl5/vendor_perl 
/usr/lib64/perl5 /usr/share/perl5 .) 
at /var/www/ +cgi-bin/result.pl line 11.

这个问题困扰了我一天多时间,最后我在perlmonks上发帖求助,解决了这个问题。原来CPAN安装模块可能会出现问题,需要手工方式安装。我在metacpan上下载了Spreadsheet::ParseExcel的源文件(tar.gz), 然后复制到/usr/local/src目录中解压进行安装,会提示需要安装其它的功能模块,逐一下载并安装。

Crypt-RC4-2.02.tar.gz
Digest-Perl-MD5-1.9.tar.gz
IO-stringy-2.111.tar.gz
OLE-Storage_Lite-0.19.tar.gz
Parse-RecDescent-1.967015.tar.gz
Spreadsheet-ParseExcel-0.65.tar.gz
Spreadsheet-WriteExcel-2.40.tar.gz

成功安装Spreadsheet::ParseExcel和Spreadsheet::Write模块后,在web中执行CGI就没有问题了。

4. 使用require引用其它perl代码,避免主程序过长。主程序中的参数能直接传递到require引用的新代码中。
5. 解决xls中文输入乱码的问题,使用以下方法进行中文转码解决

decode("utf-8",$string)

6. xls中单元格格式的获取和设置问题.
格式获取:

#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::WriteExcel;

# Open the template with SaveParser
my $parser = new Spreadsheet::ParseExcel;
my $workbook = $parser->Parse('1.xls');

my $worksheet = $workbook->worksheet(0);

my $cell = $worksheet->get_cell(7,8);
my $format = $cell->get_format();

my $pattern = $format->{Fill}->[0];
my $color1 = $format->{Fill}->[1];
my $color2 = $format->{Fill}->[2];
my $wrap = $format->{Wrap};
my $font = $format->{Font};
my $fontcolor = $font->{Color};
my $bold = $font->{Bold};
my $alignH = $format->{AlignH};
my $alignV = $format->{AlignV};;

print $color1,"\n";
print $color2,"\n";

#$workbook->close();

格式设置:

$format_cell_bdh = $workbook_bom_s12->add_format();
$format_cell_bdh->set_align('center');
$format_cell_bdh->set_align('vcenter');
$format_cell_bdh->set_bottom('1');

解决上述问题,就可以对Excel做各种操作了。为了方便,我使用了比较老的xls文件,并未测试xlsx格式。

分类: Perl 标签:

判断字符串是否为utf8

2018年2月23日 没有评论

春节放假前将批量数据导入数据库时,发现utf8报错。今天是年后上班第一天,很快找到了解决方法,将所有数据顺利导入。

return $is_utf8 if ($content !~ /[\x80-\xff]/)
分类: 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 标签: , ,