我正在尝试阅读下载的html文件
my $file = "sn.html";
my $in_fh = open $file, :r;
my $text = $in_fh.slurp;
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
Malformed UTF-8
in block <unit> at prog.p6 line 10
Run Code Online (Sandbox Code Playgroud)
如何避免这种情况并访问文件的内容?
我的一些程序由两部分组成.首先,他们从文件中读取大量数据并对其进行转换,生成Arrays,Hashes,Objects等; 第二,他们用(总是不同的)用户定义的条件分析这些数据.第一部分保持不变(只要源数据没有更改),但有时每次运行程序时都需要相当长的时间才能工作,而且我通常需要使用相同的源数据多次运行它.拥有两个程序会好得多 - 其中一个程序(一次)读取数据并对其进行转换,而另一个程序则分析它(多次).
我的问题是:存储Arrays,Hashes和Objects 的最佳方法是什么,以便第一个程序将它们写入文件,第二个程序从该文件中读取它们?
我在文档中找不到任何内容,但似乎子类中没有访问其超类的私有变量.我对吗?
class A {
has $!a;
}
class B is A {
has $.b;
method set_a($x) {
$!a = $x;
}
}
my $var = B.new();
$var.set_a(5);
say $var.a;
Run Code Online (Sandbox Code Playgroud)
这会给出一条错误消息:
Attribute $!a not declared in class B
Run Code Online (Sandbox Code Playgroud)
BTW在哪里阅读文档中的类?我只发现了一个相当短的部分类和对象.
我不完全理解文档,所以我尝试过clone,似乎有一个可变类的属性,它可以在旧对象中使用旧对象进行更改(这就是我不想要的) .如何使它们(即副本和原件)完全分开?
class A {
has @.a;
}
my A $x = A.new;
my A $y = A.new;
$x.a = 1, 2;
$y = $x.clone;
$x.a.push(4);
say $y.a; # [1 2 4]
Run Code Online (Sandbox Code Playgroud) 我们可以使用该total方法知道a中所有权重的总和Bag.
> my $b = (1,2,1).Bag
Bag(1(2), 2)
> $b.total
3
Run Code Online (Sandbox Code Playgroud)
但是,如果我们使用的%印记,而不是$我们的Bag,我们得到了一个错误信息.
> my %b = (1,2,1).Bag
{1 => 2, 2 => 1}
> %b.total
No such method 'total' for invocant of type 'Hash'. Did you mean 'cotan'?
in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
如果%b明确转换为Bag之前total,它的工作原理是:
> %b.Bag.total
3
Run Code Online (Sandbox Code Playgroud)
问题:我曾经认为,随着Set,Bag,SetHash等,采用的%印记是优选的.我错了吗?
我想,结果应该是1, 2, 3.
> my ($a, $b, $c)
> (($a, $b), $c) = ((1, 2), 3)
(((1 2) 3) (Any))
> $a, $b, $c
((1 2) 3 (Any))
Run Code Online (Sandbox Code Playgroud)
这有什么不对?
我不得不运行一个shell程序退出并出现Perl 6的错误,所以我决定测试它是如何工作的.我做了一个bash脚本产生错误,从Perl 6程序运行它:
$ cat prog.sh
echo "error" >&2
exit 1
Run Code Online (Sandbox Code Playgroud)
以下是我从Perl 6中调用它的方法:
put "start";
try {
shell "./prog.sh";
}
put "end";
Run Code Online (Sandbox Code Playgroud)
输出显示程序在运行shell命令后退出.
start
error
The spawned command './prog.sh' exited unsuccessfully (exit code: 1)
in block <unit> at b.p6 line 2
Run Code Online (Sandbox Code Playgroud)
如果我添加一个CATCH块
put "start";
try {
shell "./prog.sh";
CATCH { default {} }
}
put "end";
Run Code Online (Sandbox Code Playgroud)
一切都很好,程序工作到最后一行:
start
error
end
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:为什么有必要添加CATCH块,而try本身无法解决错误?
为什么我会得到不同的结果?
class Car {
has $.wheels;
}
my $my_car = Car.new( wheels => 4 );
say $my_car ; # Car.new(wheels => 4)
say "$my_car"; # Car<94582644384824>
put $my_car ; # Car<94582644384824>
Run Code Online (Sandbox Code Playgroud)
我想在第2和第3个案例中$my_car是字符串化的,但结果意味着什么?
不确定是否grammars要做这样的事情:我想tokens在运行时(将来 - 使用文件中的数据)定义.所以我写了一个简单的测试代码,并且按照预期它甚至都不会编译.
grammar Verb {
token TOP {
<root>
<ending>
}
token root {
(\w+) <?{ ~$0 (elem) @root }>
}
token ending {
(\w+) <?{ ~$0 (elem) @ending }>
}
}
my @root = <go jump play>;
my @ending = <ing es s ed>;
my $string = "going";
my $match = Verb.parse($string);
.Str.say for $match<root>;
Run Code Online (Sandbox Code Playgroud)
在Perl 6中做这些事情的最佳方法是什么?
我正在寻找一个更简单的解决方案.
我有一个带有相应后缀的前缀列表和一个根列表.
my @prefixes = 'A'..'E';
my @suffixes = 'a'..'e';
my @roots = 1, 2;
Run Code Online (Sandbox Code Playgroud)
我想提出的所有可能的话": A1a,B1b... A2a... E2e.
my @words;
for @roots -> $r {
for @prefixes.kv -> $i, $p {
my $s = @suffixes[$i];
my $word = [~] $p, $r, $s;
@words.push: $word;
}
}
say @words; # [A1a B1b C1c D1d E1e A2a B2b C2c D2d E2e]
Run Code Online (Sandbox Code Playgroud)
我想,这是可以做到的它更容易使用类似的zip或cross,但无法弄清楚如何?