我是perl的新手,并试图设计一个我遇到的词法分析器:
my @token_def = ( [Whitespace => qr{\s+},1],[Comment => qr{#.*\n?$}m,);
甚至在经过多个网站后我都不理解其含义.
解决方法
qr //是适用于模式匹配和相关活动的类似引号的运算符之一.
从perldoc开始:
This operator quotes (and possibly compiles) its STRING as a regular expression. STRING is interpolated the same way as PATTERN in m/PATTERN/. If
'
is used as the delimiter,no interpolation is done.
从modern_perl开始:
The qr// operator creates first-class regexes. Interpolate them into the match operator to use them:
my $hat = qr/hat/; say 'Found a hat!' if $name =~ /$hat/;
…或将多个正则表达式对象组合成复杂的模式:
my $hat = qr/hat/; my $field = qr/field/; say 'Found a hat in a field!' if $name =~ /$hat$field/; like( $name,qr/$hat$field/,'Found a hat in a field!' );