我正在浏览docs和howtos,我发现在网络套接字通信方面正确使用IO :: Select.我想我的大部分都被我的脑袋缠住了.
但是,我仍然对正确的错误处理有点模糊.假设我有类似于在对象内运行的以下代码.是的,我确实意识到它很乱,我应该将IO :: Select集成到对象而不是套接字fh本身,我不应该重新创建IO :: Select每次循环,我都在迭代什么只能永远是一个返回的文件句柄等.但是,这使示例简单.
这只是连接到服务器的客户端,但我希望能够正确处理网络级错误(如丢包).
编辑:$self->sock()只返回一个打开的IO :: Socket :: INET套接字.
sub read {
my $self = shift;
my($length) = @_; ### Number of bytes to read from the socket
my $ret;
while (length($ret) < $length) {
my $str;
use IO::Select;
my $sel = IO::Select->new($self->sock());
if (my @ready = $sel->can_read(5)) { ### 5 sec timeout
for my $fh (@ready) {
my $recv_ret = $fh->recv($str, $length - length($ret));
if (!defined $recv_ret) {
MyApp::Exception->throw(
message => "connection …Run Code Online (Sandbox Code Playgroud) 通常我会在课程__PACKAGE__->meta->make_immutable结束时通过编译完成一个课程.但是,我何时应该创建一个在运行时将角色组合成自身的类不可变?我是否应该这样做以获得更好的性能或者这是不兼容的make_immutable?make_immutable似乎加速了对象的实例化,但是一旦对象被实例化它会做什么吗?
例如,有些东西:
BUILD {
my $self = shift;
use Module::Load;
### Use the arguments passed in to determine roles applicable to
### this instance of the object. Load and apply roles.
for my $role ($self->_determine_roles()) {
load $role;
$role->meta->apply($self);
}
### $self is now a Class::MOP::Class::__ANON__... anonymous class
### Should I then be saying I'm done mutating it with something like this?
### can make_immutable even be run on an object instance and not …Run Code Online (Sandbox Code Playgroud) 我下载了5.12.3安装程序以升级我的5.12.1安装.安装5.12.3后,我以前安装的cpan模块就不见了.这是预期的行为吗?我应该在升级之前备份我的库以防止这种情况吗?
我在以下子类型和强制链中缺少什么?我希望能够强制验证类型的arrayref或死于以下输入:
假设所有类型都是完全命名空间,并且未声明的函数validate和coerce_strvalidate分别验证(返回bool)和强制并从输入返回有效字符串.
subtype 'CustomType'
=> as 'Str'
=> where { validate($_) }
;
coerce 'CustomType'
=> from 'Str'
=> via { if (my $coerced = coerce_str($_)) {
return $coerced;
}
return $_;
}
;
subtype 'ArrayRefofCustomTypes'
=> as 'ArrayRef[CustomType]'
;
coerce 'ArrayRefofCustomTypes'
=> from 'CustomType'
=> via { [ $_ ] }
;
has 'values' => ( is => 'ro', required => 1,
isa => 'ArrayRefofCustomTypes',
coerce => 1,
);
Run Code Online (Sandbox Code Playgroud)
我知道CustomType有效; 因为我可以定义一个属性,并使用强制字符串或已经有效的字符串初始化对象.我不确定该怎么做的是显式地处理从构造函数中传递到传递的arrayref并单独验证所有包含的字符串.我已经阅读了有关深度强制的文档(http://search.cpan.org/dist/Moose/lib/Moose/Manual/Types.pod#Deep_coercion …