Perl 帮我看看 my %keep = map { + $_ => 1 } @files_to_keep;

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 12:14:35
Perl帮我看看my%keep=map{+$_=>1}@files_to_keep;Perl帮我看看my%keep=map{+$_=>1}@files_to_keep;Perl帮我看看my%keep=

Perl 帮我看看 my %keep = map { + $_ => 1 } @files_to_keep;
Perl 帮我看看 my %keep = map { + $_ => 1 } @files_to_keep;

Perl 帮我看看 my %keep = map { + $_ => 1 } @files_to_keep;
由于map{...后可接BLOCK(代码块)和EXPR(子函数、正则表达式),+是用来语法提示,告诉编译器下边的是代码块,防止编译出现语法错误.这个哈希数组应该就是保护@files_to_keep的文件,防止误删.
{ starts both hash references and blocks,so map { ...could be either the start of map BLOCK LIST or map EXPR,LIST.Because Perl doesn't look ahead for the closing } it has to take a guess at which it's dealing with based on what it finds just after the {.Usually it gets it right,but if it doesn't it won't realize something is wrong until it gets to the } and encounters the missing (or unexpected) comma.The syntax error will be reported close to the },but you'll need to change something near the { such as using a unary + to give Perl some help:
%hash = map { "\L$_" => 1 } @array # perl guesses EXPR.wrong
%hash = map { +"\L$_" => 1 } @array # perl guesses BLOCK.right
%hash = map { ("\L$_" => 1) } @array # this also works
%hash = map { lc($_) => 1 } @array # as does this.
%hash = map +( lc($_) => 1 ),@array # this is EXPR and works!
%hash = map ( lc($_),1 ),@array # evaluates to (1,@array)