lis*_*tor 3 inheritance grammar capture named perl6
我正在尝试解析csv文件以执行简单的操作:提取姓氏,ID和生日,并将生日格式从m/d/yyyy更改为yyyymmdd.
(1)我在生日时使用了命名捕获,但似乎没有调用命名的捕获方法来制作我想要的东西.
(2)继承语法动作方法似乎不适用于命名捕获.
我做错了什么?
my $x = "1,,100,S113*L0,35439*01,John,JOE,,,03-10-1984,47 ELL ST #6,SAN FRANCISCO,CA,94112,415-000-0000,,5720,Foo Bar,06-01-2016,06-01-2016,Blue Cross,L,0,0";
# comma separated lines
grammar insurCommon {
regex aField { <-[,]>*? }
regex theRest { .* }
}
grammar insurFile is insurCommon {
regex TOP { <aField> \,\, # item number
<aField> \, # line of business
<aField> \, # group number
<ptID=aField> \, # insurance ID,
<ptLastName=aField> \, # last name,
<aField> \,\,\, # first name
<ptDOB=aField> \, # birthday
<theRest> }
}
# change birthday format from 1/2/3456 to 34560102
sub frontPad($withWhat, $supposedStrLength, $strToPad) {
my $theStrLength = $strToPad.chars;
if $theStrLength >= $supposedStrLength { $strToPad; }
else { $withWhat x ($supposedStrLength - $theStrLength) ~ $strToPad; }
}
class dateAct {
method reformatDOB($aDOB) {
$aDOB.Str.split(/\D/).map(frontPad("0", 2, $_)).rotate(-1).join;
}
}
class insurFileAct is dateAct {
method TOP($anInsurLine) {
my $insurID = $anInsurLine<ptID>.Str;
my $lastName = $anInsurLine<ptLastName>.Str;
my $theDOB = $anInsurLine<ptDOB>.made; # this is not made;
$anInsurLine.make("not yet made"); # not yet getting $theDOB to work
}
method ptDOB($DOB) { # ?ptDOB method is not called by named capture?
my $newDOB = reformatDOB($DOB); # why is method not inherited
$DOB.make($newDOB);
}
}
my $insurAct = insurFileAct.new;
my $m = insurFile.parse($x, actions => $insurAct);
say $m.made;
Run Code Online (Sandbox Code Playgroud)
输出是:
===SORRY!=== Error while compiling /home/test.pl
Undeclared routine:
reformatDOB used at line 41
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助 !!!
您正在尝试调用不存在的子例程reformatDOB,而不是方法.
与Java相比,Perl6不允许您省略调用,即方法调用必须写为
self.reformatDOB($DOB)
Run Code Online (Sandbox Code Playgroud)
另外,还有简写形式
$.reformatDOB($DOB) # same as $(self.reformatDOB($DOB))
@.reformatDOB($DOB) # same as @(self.reformatDOB($DOB))
...
Run Code Online (Sandbox Code Playgroud)
另外对返回值施加上下文.
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |