为什么?:运营商无法返回列表?
my $hash = {
...
($row->active?checked=>1:()),
};
Run Code Online (Sandbox Code Playgroud)
该DOC只字不提标量或列表上下文
UPD
另一个例子:
@list = 2,3; # CORRECT
@list = 1? 2,3 : (); # Syntax error
Run Code Online (Sandbox Code Playgroud)
为什么第一个好,但第二个不是?似乎perl不应该只是传播2,3到外部上下文的问题;
我写了一个更新挂钩(服务器端),它检查所有提交消息(检查是否存在问题ID)
有一个我的python代码(update.py)的摘录:
[...]
if newrev == "0000000000000000000000000000000000000000":
newrev_type = "delete"
elif oldrev == "0000000000000000000000000000000000000000":
newrev_type = "create"
# HERE IS MY QUESTION, I want to get the commits SHA-1 :-)
else:
POPnewrev_type = os.popen("git cat-file -t " + newrev)
newrev_type = POPnewrev_type.read()[0:-1]
# get the SHA-1
POPanalyzelog = os.popen("git log " + oldrev + ".." + newrev + " --pretty=#%H")
analyzelog = POPanalyzelog.read().split('#')
[...]
Run Code Online (Sandbox Code Playgroud)
所以,在这里,如果newrev_type ="delete",用户想要删除分支=>没问题.
在推入现有分支的情况下,我们得到提交的SHA-1 => OK
但是当用户创建分支时,我不知道如何获得SHA-1 ...
你有什么想法?
我有脚本:
#!/usr/bin/env perl
sub t0 {
return; # We return nothing for ERROR
}
sub t1 {
@z = ();
return @z; # We return array (which maybe empty) for no ERROR
}
Run Code Online (Sandbox Code Playgroud)
在标量上下文中,我可以区分错误和OK状态:
my $x1 = t0(); # undef
my $x2 = t1(); # 0
Run Code Online (Sandbox Code Playgroud)
在列表上下文中调用时,有没有办法区分从子例程返回的错误和空数组?
my @x1 = t0(); # empty list
my @x2 = t1(); # empty list
Run Code Online (Sandbox Code Playgroud)
我觉得我需要"0E0"(零但是为真)但是对于列表上下文.
我有应用它工作正常.但是现在我们决定把它移到/api路上.所以我绕道而行
my $r = $self->routes;
# Application is always under /api/v1, /api/v2 etc. path
$r = $r->any( '/api/:api', [ api => qr/v\d+/ ] )->detour( 'MyApp' );
$r->get( '/users/me' )->to( 'user#show_me' );
Run Code Online (Sandbox Code Playgroud)
但在此之后没有任何作用.要求site.domain/api/v1使应用程序陷入无限循环.
还有Mojolicious :: Plugin :: Mount,但它只对在指定路径下安装另一个应用程序很有用.
本指南也无法解决问题.
我可以通过切片获得值:
($x, $y, $z) = $hash->{ key }->@[0,1,2]
Run Code Online (Sandbox Code Playgroud)
为什么我不能写?
($x, $y, $z) = $hash->{ key }->@*
Run Code Online (Sandbox Code Playgroud)
对于key未在哈希中定义的情况下的第二个表达式,我得到错误:
不能使用未定义的值作为ARRAY参考...
执行perl程序我得到不同的结果:
$ perl -e 'my $i = 2; $i += 2 + $i++; print "$i\n"'
7
$ perl -e 'my $i = 2; $i += $i + $i++; print "$i\n"'
8
Run Code Online (Sandbox Code Playgroud)
为什么结果不同?在第二种情况下我错过了什么?我希望7在这两种情况下.
我有两个查询。我希望两者都插入相同的值:429496729600,但是其中一个由于错误而失败:
db=> update order_detail set amount = 400*1024*1024*1024 where id = 11;
ERROR: integer out of range
db=> update order_detail set amount = 429496729600 where id = 11;
UPDATE 1
Run Code Online (Sandbox Code Playgroud)
为什么第一次查询会发生错误?
UPD
忘记指定amountis bigint和
400*1024*1024*1024 == 429496729600
Run Code Online (Sandbox Code Playgroud) 当我运行这个程序时:
print(rand*100)
Run Code Online (Sandbox Code Playgroud)
我从[0,1)范围获得价值.
但为此:
print(100*rand)
Run Code Online (Sandbox Code Playgroud)
我从[0,100)范围获得价值.
什么是优先权?为什么第一个表达式不返回[0,100)范围的值?
例如,如果我有下一个 perl 脚本:
use strict;
use warnings;
print $x;
Run Code Online (Sandbox Code Playgroud)
当我运行此脚本时,编译将失败并显示错误:
Global symbol "$x" requires explicit package name (did you forget to declare "my $x"?) at ...
Run Code Online (Sandbox Code Playgroud)
是否可以编写一些在发生此错误时调用的 perl 模块并自动修复此错误并继续编译?(即使链接到任何信息都可以)
# This code is incorrect.
# Here I just ask about such ability
# This code is very weak approximation how it might look
package AutoFix;
sub fix {
$main::x = 'You are defined now';
}
1;
Run Code Online (Sandbox Code Playgroud)
所以下一个代码不会失败并打印You are defined now:
use strict;
use warnings;
use AutoFix;
print …Run Code Online (Sandbox Code Playgroud) 我想匹配Internet.或Internet在字符串的末尾。
我可以写:
$str =~ m/Internet\.|Internet$/
Run Code Online (Sandbox Code Playgroud)
有regex没有不重复的写法Internet?