- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Perl 5 中,我可以创建一个字符串的文件句柄,并从字符串中读取或写入,就像它是一个文件一样。这非常适合处理测试或模板。
例如:
use v5.10; use strict; use warnings;
my $text = "A\nB\nC\n";
open(my $fh, '<', \$text);
while(my $line = readline($fh)){
print $line;
}
# Warning: This code does not work
use v6;
my $text = "A\nB\nC\n";
my $fh = $text;
while (my $line = $fh.get ) {
$line.say;
}
# Warning: Example of nonfunctional code
No such method 'get' for invocant of type 'Str'
in block <unit> at string_fh.p6:8
open(my $fh, '<', \$text)
并不奇怪与 Perl6 的不一样
my $fh = $text;
.所以问题是:如何从 Perl 6 中的字符串创建虚拟文件句柄,如
open(my $fh, '<', \$str)
在 Perl 5 中?或者这是尚未实现的东西?
use v5.10; use strict; use warnings;
my $text = "";
open(my $fh, '>', \$text);
print $fh "A";
print $fh "B";
print $fh "C";
print "My string is '$text'\n";
My string is 'ABC'
最佳答案
读
逐行阅读的惯用方式是 .lines
method ,可在 Str
上使用和 IO::Handle
.
它返回一个惰性列表,您可以将其传递给 for
,如
my $text = "A\nB\nC\n";
for $text.lines -> $line {
# do something with $line
}
my $scalar;
my $fh = IO::Handle.new but
role {
method print (*@stuff) { $scalar ~= @stuff };
method print-nl { $scalar ~= "\n" }
};
$fh.say("OH HAI");
$fh.say("bai bai");
say $scalar
# OH HAI
# bai bai
use IO::Capture::Simple;
my $result;
capture_stdout_on($result);
say "Howdy there!";
say "Hai!";
capture_stdout_off();
say "Captured string:\n" ~$result;
关于string - 我可以在 Perl 5 中为字符串创建文件句柄,我如何在 Perl 6 中做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28702850/
我设置了 Helm 柄和 Helm 柄。我有tiller-deploy。昨天,我可以定期运行了。但今天我收到此错误消息 Error: could not find a ready tiller pod
我以前已将分er安装到特定的 namespace 中。 我设置了一个环境变量来设置'tiller'命名空间-但我不记得该环境变量的名称-而且似乎无法通过网络搜索找到它。 这是什么 key ? 最佳答案
当我在 View 模型中使用如下界面时 class MainViewModel @ViewModelInject constructor( private val trafficImagesR
我正在尝试找到如何在某个 fragment 相关场景中定义 Hilt 的解决方案。我有以下设置: Activity 父 fragment 1 子 fragment 1 子 fragment 2 ...
Hilt 指出如果没有@Provides 注解就不能提供这个接口(interface): interface PlannedListRepository { fun getAllLists()
我的问题非常简单明了:两个注释/示例之间有什么区别: 例子一 @Singleton class MySingletonClass() {} @Module @InstallIn(FragmentCom
我是一名优秀的程序员,十分优秀!