我有一些模块,并希望为某些sub制作别名.这是代码:
#!/usr/bin/perl
package MySub;
use strict;
use warnings;
sub new {
my $class = shift;
my $params = shift;
my $self = {};
bless( $self, $class );
return $self;
}
sub do_some {
my $self = shift;
print "Do something!";
return 1;
}
*other = \&do_some;
1;
Run Code Online (Sandbox Code Playgroud)
它有效,但它会产生编译警告
名称"MySub :: other"仅使用一次:/tmp/MySub.pm第23行可能输入错误.
我知道我可以输入no warnings 'once';,但这是唯一的解决方案吗?为什么Perl警告我?我究竟做错了什么?
我有一段代码,它使用了大量的实验中(when,smartmatch,given)的特点,这不是我的代码,我不希望看到很多关于实验性功能的警告.所以我添加no warnings 'experimental';了这段代码.但是experimental只在perl 5.18中添加了类别,所以我的代码在旧的perls中死掉了:
未知警告类别'实验'
如何禁用此警告并且不要破坏旧版perls中的代码?
让我们想象一下,我们是perl的新手并写了一些很棒的模块MyModule.pm.我们还写了一些很棒的脚本myscript.pl,需要使用这个模块.
use strict;
use warnings;
use MyModule;
etc...
Run Code Online (Sandbox Code Playgroud)
现在我们将创建dir /home/user/GreatScript并将我们的文件放入其中.并试图运行myscript.pl...
cd /home/user/GreatScript
perl myscript.pl
Run Code Online (Sandbox Code Playgroud)
大!现在搬到另一个目录......
cd /
perl /home/user/GreatScript/myscript.pl
Run Code Online (Sandbox Code Playgroud)
获取一些不太有用的错误@INC和路径列表.这是什么?现在经过一些谷歌搜索我们知道@INC包含搜索我们的模块的路径,这个错误意味着Perl找不到MyModule.pm.
现在我们可以:
@INC
将我们的模块安装到其中一个目录的开头@INC@INC在BEGIN添加use lib '/home/user/GreatScript';到我们的脚本部分中手动添加我们的路径,但它看起来很糟糕.如果我们将脚本移动到其他目录怎么办?FindBin模块来查找我们当前的目录,并使用此路径use lib "$FindBin::Bin";,但它不是100%有效,例如mod_perl或问题的一些错误......abs_path方法从中提取路径Cwd.看起来像又一辆自行车?为什么这种明显和常规的操作需要大量的工作?如果我有一个脚本,那很容易......但是为什么我需要在我的所有脚本中编写这几行额外代码?它不会100%工作!为什么perl @INC默认情况下不添加当前脚本目录,就像"."一样?
PS:我正在寻找我的问题的答案,但只找到上面列表中的解决方案列表和其他一些解决方案.我希望这个问题重复......
我正在尝试使用格式为的日期字符串:
YYYY-MM-DDTHH:MM:SS
用于生成此字符串的strftime格式为%FT%T:
perl -MPOSIX=strftime -E 'say strftime q{%FT%T}, localtime;'
Run Code Online (Sandbox Code Playgroud)
但是当我尝试用Time :: Piece解析这个日期字符串时:
perl -MPOSIX=strftime -MTime::Piece -E '
say strftime q{%FT%T}, localtime;
Time::Piece->strptime( strftime(q{%FT%T}, localtime), q{%FT%T});
'
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
2013-11-15T17:32:58 Error parsing time at /usr/local/lib/perl/5.14.2/Time/Piece.pm line 469.
或者如果我试试这个:
perl -MPOSIX=strftime -MTime::Piece -E 'Time::Piece->strptime(strftime(q{%F}, localtime), q{%F});'
Run Code Online (Sandbox Code Playgroud)
我懂了:
garbage at end of string in strptime: 2013-11-15 at /usr/local/lib/perl/5.14.2/Time/Piece.pm line 469.
在man中说%F是strptime和strftime的ISO8601日期格式.strptime向后兼容strftime吗?
我想使用perl + PDF :: API2在我的网站上生成一些pdf文档.
1)阅读pdf模板
2)向模板添加一些数据
3)将文件保存到硬盘
4)读取新文件
5)将其打印到用户的浏览器
#!/usr/bin/perl
use strict;
use PDF::API2;
my $p = PDF::API2->open('/way/to/input/pdf.pdf');
my $font = $p->ttfont('/way/to/font.ttf', -encode => 'utf8');
my $page = $p->openpage(1);
my $text = $page->text();
$text->font($font,12);
$text->translate(150,150);
$text->text('Hello world!');
$p->saveas('/way/to/out/pdf.pdf'); #ex: '/usr/local/www/apache22/data/docs'
my $fileContent;
open(my $file, '<', '/way/to/out/pdf.pdf') or die $!;
binmode($file);
{
local $/;
$fileContent = <$file>;
}
close($file);
binmode STDOUT;
print "Content-type: application/pdf\n\n";
print $fileContent;
exit;
Run Code Online (Sandbox Code Playgroud)
如何在不将临时PDF存储在/ way/to/out /文件夹(r/w可访问)的情况下执行此操作?
我在div上有双击事件处理程序,并在按钮上单击事件处理程序,即div的子项.但是当我点击按钮2次时,会发生div的dblclick事件(参见http://jsfiddle.net/9dcSk/3/).
<!DOCTYPE html>
<html>
<head>
<script src="/bs21/js/jquery-1.10.1.min.js"></script>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
body,html {
background:yellow;
}
.myDiv {
background:pink;
padding:40px;
}
</style>
<script>
$( document ).ready( function() {
$('div.myDiv').click( function() {
console.log('div dbl clicked');
} );
$('button.myButton').click( function(event) {
console.log('button clicked');
} );
});
</script>
</head>
<body>
<div class=myDiv>test<button class=myButton>ok</button></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我试图添加e.stopPropagation()到click事件,但这没有帮助.