我刚刚安装了Perl 5.18,我收到很多这样的警告,
given is experimental at .\[...].pl line [...].
when is experimental at .\[...].pl line [...].
Smartmatch is experimental at C:/strawberry/perl/site/lib/[...] line [...].
Run Code Online (Sandbox Code Playgroud)
看看这些警告 - 我从未在任何地方听到过这些警告 - 我只能在两个地方找到这个警告,
Perl Delta仍然能够最大限度地提及这些功能正在发生的事情,它被埋在吊舱中的一半,
在v5.10.0中添加并在v5.10.1中进行了重大修订的智能匹配一直是一个常见的投诉点.尽管有许多方法可用,但它也证明了Perl的用户和实现者都存在问题和困惑.关于如何最好地解决问题,已经提出了许多建议.很明显,smartmatch几乎肯定会在将来改变或消失.不建议依赖其当前行为.现在,当解析器看到〜,给定或何时发出警告.
我对过去10年中Perl最显着的变化如何被拉动感到困惑.我已经开始使用given
,when
以及smartmatch
所有地方.还有关于这些未来的更多信息吗?怎么有人发现它们"令人困惑?" 这些功能如何变化?是否有计划使用模块实现这些功能?
Run Code Online (Sandbox Code Playgroud)@array ~~ $scalar is true when $scalar is in @array
到draegtun回复:
从5.10.1+开始,~~的顺序很重要.因此它需要是$ scalar ~~ @ array
关于~~
源(s)的链接的小型入门如何包括以下具体问题:什么是~~
?什么~~
叫?为什么订单在一个版本中很重要但在之前的版本中却不重要?
请注意,一个好的摘要可能无法获得所有细节,可能很难写.引入或引用对于为不熟悉的人节省时间非常有用,~~
同时扩展此Perlism的曝光度.
搜索字符串:non-word-tilde-tilde
non-word-at-sign
.
我想重复搜索不会更改的数组中的值.
到目前为止,我一直在这样做:我把值放在一个哈希(所以我有一个数组和一个基本相同内容的哈希),然后我使用搜索哈希exists
.
我不喜欢有两个不同的变量(数组和散列)都存储相同的东西; 但是,哈希搜索速度要快得多.
我发现~~
Perl 5.10 中有一个(smartmatch)运算符.在数组中搜索标量时效率如何?
我有一个数组,以下测试返回true:
1 ~~ @a
Run Code Online (Sandbox Code Playgroud)
然而,以下测试返回false:
@a ~~ 1
Run Code Online (Sandbox Code Playgroud)
我在Learning Perl中读到,智能匹配运算符两侧的值的放置并不重要,但显然在上面的代码中它确实如此.这是为什么?这两个陈述是否检查了不同的内容?
我想提取行键(这里是28_2820201112122420516_000000
)、列名(这里是bcp_startSoc
)和值(这里是64.0
)$str
,其中$str
是 HBase 中的一行:
# `match` is OK
my $str = '28_2820201112122420516_000000 column=d:bcp_startSoc, timestamp=1605155065124, value=64.0';
my $match = $str.match(/^ ([\d+]+ % '_') \s 'column=d:' (\w+) ',' \s timestamp '=' \d+ ',' \s 'value=' (<-[=]>+) $/);
my @match-result = $match».Str.Slip;
say @match-result; # Output: [28_2820201112122420516_000000 bcp_startSoc 64.0]
# `smartmatch` is OK
# $str ~~ /^ ([\d+]+ % '_') \s 'column=d:' (\w+) ',' \s timestamp '=' \d+ ',' \s 'value=' (<-[=]>+) $/
# …
Run Code Online (Sandbox Code Playgroud) 以下脚本智能匹配两个数组的切片.一开始,两个阵列都是一样的,我得到了合理的结果.然后我改变其中一个数组并智能匹配两个新切片,但它仍然说切片是相同的.但是,当我将切片复制到数组中时,对数组进行智能匹配表明它们确实不同.
剧本:
#!/usr/bin/perl
use warnings;
use strict;
use diagnostics;
my @x = qw (one two);
my @y = qw (one two);
my @x_s;
my @y_s;
print "Before change: values are the same:\n";
@x_s = @x[0,1];
@y_s = @y[0,1];
print "\@x_s: @x_s\n";
print +(@x[0,1] ~~ @y[0,1]) ? "equal\n" : "not equal\n";
print +(@x_s ~~ @y_s) ? "equal\n" : "not equal\n";
$x[0]='three';
print "After change: values should be different:\n";
@x_s = @x[0,1];
@y_s = @y[0,1];
print "\@x_s: @x_s\n";
print +(@x[0,1] ~~ @y[0,1]) …
Run Code Online (Sandbox Code Playgroud) 在阅读和尝试签名智能匹配时,我遇到了一些奇怪的事情。
执行以下 smartmaching 签名对:
my @sigs = :($a, $b), :($a, @b), :($a, %b);
my @signatures_to_check = :($, $), :($, @), :($, %);
my $c = 0;
for @sigs -> $sig {
for @signatures_to_check -> $s {
$c++;
if $sig ~~ $s {
say " [ $c ] " ~ $sig.gist ~ ' match ' ~ $s.gist;
next;
}
say " [ $c ] " ~ $sig.gist ~ ' do NOT match ' ~ $s.gist;
}
say "\n" ~ '#' x …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用smartmatch运算符将简单字符串与正则表达式模式匹配:
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use open qw(:std :utf8);
my $name = qr{/(\w+)/};
my $line = 'string';
print "ok\n" if $line ~~ /$name/;
Run Code Online (Sandbox Code Playgroud)
我希望这打印"确定",但事实并非如此.为什么不?
我有
@a = (1,2,3); print (@a ~~ (1,2,3))
Run Code Online (Sandbox Code Playgroud)
和
@a = (1,2,3); print (@a == (1,2,3))
Run Code Online (Sandbox Code Playgroud)
第一个是我期望的工作,但它不会打印任何东西.第二个打印1.
为什么?是不是智能匹配运营商~~
应该匹配的情况@a ~~ (1,2,3)
?
为什么 smartmatch 运营商~~
说这0
不在(0, 5..100)
?
print ((0 ~~ (0, 5..100)) ? "Y" : "N");
Run Code Online (Sandbox Code Playgroud)
N
我正在测试一个返回数组的函数.根据环境,数组可能会有所不同,但它总是至少有一个常量值(我要测试的值).
当我使用Perl 5.12时,我可以使用smartmatch运算符来查找元素是否在数组中:
ok($known_value ~~ @returned, 'testing method abc')
Run Code Online (Sandbox Code Playgroud)
但我喜欢的增强的输出is
,并like
与"发现"和"预期"的部分.所以我尝试了这个:
cmp_ok($known_value, '~~', @returned, 'testing method abc')
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为它似乎cmp_ok
在比较的两个部分中都需要标量:
not ok 1 - testing method abc
# Failed test 'testing method abc'
# at abc.t line 53.
# 'stable_value'
# ~~
# '2'
Run Code Online (Sandbox Code Playgroud)
"预期"槽中的数组在标量上下文中计算并转换为2.
我可以使用hack使用like
并对数组进行字符串化来解决这个问题,但是在测试中你可以使用smartmatch操作符作为比较方法(比如when
)会很好.有没有办法用Test :: More或其他模块做到这一点?
目前我正在使用:
ok($known_value ~~ @returned, 'testing method abc')
or diag (
"ERROR:\n".
"Found: ". Dumper @returned."\n".
"Expected at least one element equal to '$known_value'"
)
Run Code Online (Sandbox Code Playgroud)
这是我能做的最好的吗?
我很喜欢编程Perl; 我在搜索我从外部文本文件制作的数组时遇到了困难.我正在寻找一种简单的方法来检查用户条目是否位于数组中.我之前使用过智能匹配功能,但从未使用"if"语句,似乎无法使其工作.我是否实现了这个函数错误,或者是否有更简单的方法来检查用户的字符串是否在数组中?
#!/usr/bin/perl
use 5.010;
#Inventory editing script - Jason Black
#-------------------------------------------------------------------------------
print "1. Add Items\n";
print "2. Search Items\n";
print "Please enter your choice: ";
chomp ($userChoice = <STDIN>); #Stores user input in $userChoice
if($userChoice == 1){
$message = "Please enter in format 'code|title|price|item-count'\n";
&ChoiceOne;
}
elsif($userChoice == 2){
$message = "Enter search terms\n";
&ChoiceTwo;
}
sub ChoiceOne{
print "$message\n";
chomp($userAddition = <STDIN>); #Stores input in $userAddition
$string1 = "$userAddition";
open (FILE, "FinalProjData.txt") or die ("File not found"); #"FILE" …
Run Code Online (Sandbox Code Playgroud)