1、读文件
- #001一次读入全部内容
- my $string = slurp 'sample.txt';
- say $string;
- #002按行读入文件内容
- for open('sample.txt').lines
- {
- .say;
- }
- #003按行读入文件内容
- my @content = lines ('sample.txt');
- say @content.join("\n");
2、写文件
- my $fh =open "sample.txt",:a;
- $fh.print('foo');
- $fh.say('bar');
- $fh.close;
- say slurp('sample.txt')
3、printf、format
- say '100.125'.fmt('%.2f');
- printf '%.2f','100.125';
4、like #List::Util qw(reduce ) in the perl5
- my @ary = 1,2,3,4,5;
- say 'Sum: ', [+] @ary;
- say 'Product: ', [*] @ary;
5、(2、4、6、8)
- for [2, *+2 ... 8] ->$x{
- say $x;
- }
- say [2, *+2 ... 8];
6、字符类型
- my $x; say $x.WHAT; # Any()
- my Int $y; say $y.WHAT; # Int()
- my Str $z; say $z.WHAT; # Str()
7、like my $str =<> in the perl5
- my $str = prompt("Enter a string: ");
8、绑定
- my $x = 'foo';
- #like {my $y=\$x} in perl5
- my $y := $x;
- $y = 'bar';
- say $x; # 'bar'
9、上下文
- my @array = 1, 2, 3;
- # generic item context
- my $a = @array; say $a.WHAT; # prints Array
- # string context
- say ~@array; # "1 2 3"
- # numeric context
- say +@array; # 3
- # boolean context
- my $is-nonempty = ?@array; say $is-nonempty;
阅读(1985) | 评论(2) | 转发(0) |