我正在使用配置文件(在YAML中)来定义稍后用于验证我的应用程序所需的其他配置值的类型:
---
action: >
use List::MoreUtils;
my $value = $_;
any { $value eq $_ } qw(fatal keep merge non-fatal replace);
dir : return defined $_ ? -d $_ : -1;
file : return defined $_ ? -f $_ : -1;
string: 1;
---
config-element:
value: foo
type : file
etc ...
Run Code Online (Sandbox Code Playgroud)
我们的想法是eval每个类型定义,将它们抛入哈希,然后调用验证配置数据(以下是易于理解的示意图):
#throw sub refs into hash
my %type_sub;
foreach my $key (keys %$type_def_ref) {
my $sub_str = "sub {$type_def_ref->{$key}}";
$type_sub{$key} = eval $sub_str;
}
#validate (myfile …Run Code Online (Sandbox Code Playgroud) 这段代码:
foreach my $file (@data_files) {
open my $fh, '<', $file || croak "Could not open file $file!\n";
my @records = <$fh>;
close $fh;
....
}
Run Code Online (Sandbox Code Playgroud)
产生此错误:
readline() on closed filehandle $fh at nut_init_insert.pl line 29.
Run Code Online (Sandbox Code Playgroud)
而且我不知道为什么.
编辑:原始帖子在open声明中有','而不是'<' .
假设您正在运行某些单元测试,并且您想要查看您正在测试的方法(或脚本或函数或其他)是否应该失败.你如何设置这样的测试?我希望这样的事情:
ok( $obj->method($my, $bad, $params) == DEATH, 'method dies as expected');
Run Code Online (Sandbox Code Playgroud)
虽然我没有看到它如何工作,因为method die传递坏参数和测试脚本停止.
还有另外一种方法吗?
我正在使用Moose(特别MooseX::Declare)创建一个迭代器对象,Iter该对象具有一个next提升状态和返回的方法,0或者1根据需要在while语句中使用.我遇到的问题是,根据其中一个构造参数的存在,next需要执行两组非常不同的操作.我看到它的方式我有五个选择:
next方法中1号只是业余爱好者.
我想,2号是正确的OOP做事方式.我不反对这样做,但似乎有点矫枉过正,只是覆盖一个方法.
我过去常常在程序上或伪功能上使用3号,这就是我现在正在做的事情.
4号是,大家都知道,充满了危险,我一无所知穆斯胆量要开始瞎搞不必要的时候.
最后一项,第5项在我看来是最明智的(和Perlish)但是像2号一样,有点太多的工作.我真的很想知道是否有第五种方法我没有考虑过如此挂钩到元类或者可能是一个MooseX相当成熟的模块.
我正在使用PuTTY访问我的BSD文件服务器,我有几个终端窗口一次打开.我写了一个简单的命令文件来自动打开终端窗口,我想知道是否可以将它们放在特定的位置.
我有两个显示器,就像现在一样,我必须手动将终端窗口拖到我的第二个显示器上.
我正在使用Getopt :: Lucid来处理CLO,我遇到了一个有趣且意想不到的问题.以下代码:
push @clo_spec, map { Switch($_) } qw(-c -m -s -p),
map { Switch($_) } qw(--help --man --usage --version),
map { Switch($_) } qw(--debug --verbose),
map { Param($_) } keys %$rc_spec_ref
;
my $clo_o = Getopt::Lucid->getopt(\@clo_spec);
Run Code Online (Sandbox Code Playgroud)
生成以下错误:
'Getopt::Lucid::Spec=HASH(0x9383847)' is not a valid option name/alias
Run Code Online (Sandbox Code Playgroud)
现在,通过引用表示有效选项的字符串表达式来配置Getopt :: Lucid,然后将这些字符串传递给返回祝福哈希的六个子例程之一.每个子程序代表一种选项; 开关,计数器,参数,列表或键对.
有趣的是,如果删除任何三个地图表达式,
push @clo_spec, #map { Switch($_) } qw(-c -m -s -p),
map { Switch($_) } qw(--help --man --usage --version),
#map { Switch($_) } qw(--debug --verbose),
#map { Param($_) } keys …Run Code Online (Sandbox Code Playgroud) 标题问题很简单; 我正在寻找类似的选项,magic以便Vim正则表达式足够Perly.我已经厌倦了在\v需要做任何捕捉的时候打字.
我在我的.emacs文件中定义的键绑定不起作用.这是文件:
;init modes
(menu-bar-mode 0)
(tool-bar-mode 0)
(cua-mode)
(column-number-mode)
(fset 'perl-mode 'cperl-mode)
(cperl-set-style PerlStyle)
;keymappings
(global-set-key [f12] 'save-buffer)
(global-set-key [S-f12] 'write-file)
(global-set-key [f7] 'ispell)
(global-set-key [up] 'scroll-one-line-up)
(global-set-key [down] 'scroll-one-line-down)
;functions
(defun scroll-one-line-up (&optional arg)
(interactive "p")
(scroll-up (or arg 1)))
(defun scroll-one-line-down (&optional arg)
(interactive "p")
(scroll-down (or arg 1)))
Run Code Online (Sandbox Code Playgroud)
我知道Emacs解析文件,因为其他一切似乎都有效.这只是键没有被束缚.
我怎样才能使它工作?
perl ×5
batch-file ×1
emacs ×1
eval ×1
map ×1
moose ×1
parameters ×1
parsing ×1
putty ×1
regex ×1
subroutine ×1
tap ×1
testing ×1
vim ×1
windows ×1