以下脚本智能匹配两个数组的切片。一开始,两个数组都是相同的,我得到了合理的结果。然后我更改其中一个数组并智能匹配两个新切片,但它仍然说切片是相同的。但是,当我将切片复制到数组中时,智能匹配数组表明它们确实不同。
剧本:
#!/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]) ? "equal\n" : "not equal\n";
print +(@x_s ~~ @y_s) ? "equal\n" : "not equal\n";
输出:
Before change: values are the same:
@x_s: one two
equal
equal
After change: values should be different:
@x_s: three two
equal
not equal
我正在使用 Perl 5.10.1,这发生在数组切片和哈希切片上。为什么会这样?