89、编写一个语法来匹配点分十进制 IP 地址,并编写动作类将其转换为对应的整数值,使用给定的 IP 地址 192.168.1.137 进行测试
以下是实现该功能的代码:
grammar DottedDecimal {
token TOP { <digits> ** 4 % '.' }
regex digits { ( <[0..9]> ** 3 ) <?{ 0 <= $0 <= 255 }> }
}
class DottedDecimal::SimpleActions {
method TOP ($/) {
make [+] (
$<digits>.[0] +< 24,
$<digits>.[1] +< 16,
$<digits>.[2] +< 8,
$<digits>.[3]
);
}
}
my $string = '192.168.1.137';
my $match = DottedDecimal.parse( $string, :actions(DottedDecimal::SimpleActions));
say $match;
say $match.made.fmt('%X');
这段代码定义了一个语法 DottedDecimal 来匹配点分十进制 IP 地址,然后定义了一个动作类 DottedDecimal::SimpleActions 将匹配到的 IP 地址转换为对应的整数值。最后使用给定的 IP 地址 192.168.1.137 进行测试,并输出匹配结果和转换后的整数值的十六进制表示。
90、编写代码,使用 Supplier 来发射值,并使用 tap 来读取这些值
以下是示例代码:
<

1170

被折叠的 条评论
为什么被折叠?



