我在 Perl v5.26 和 Linux 下使用 Moo (OO)。
我正在撰寫測驗并在物件中有某種運行時字典來存盤應用程式的狀態。我想測驗定義的代碼參考是否指向該物件中的適當方法。
我的問題是為這個測驗找到正確的表達方式。
# Struggle point
ok($exitFun == \$TK->exitError);
diag($exitFun," "); # OK .. CODE(0x55a8a9027808)
diag(\$TK->exitError,"\n"); # ERROR .. Too few args for AppTK::exitError
細節
應用程式工具包在運行時通過幾個步驟進行初始化,以標準化一些東西。這是設定:
# ---------------------------------------------------
# Support variables
# ----------------------------------------------------
my $TOOL = 'TEST.TOOL';
my $MAGIC = 'TEST.MAGIC';
my $IOTYPE = 'ASCII.DICT';
my $VERSION = 'VERSION';
my $AUTHOR = 'AUTHOR';
my $CONTACT = 'CONTACT';
my $OUTPUT = 'TEST.CASE';
my $RES_ERROR = 'ERROR';
# Some paremter with certain types
my $OPT_STR = 'OPT_STR';
my $OPT_INT = 100;
my $OPT_REAL = 1.8;
my $OPT_BOOL = 0;
my $OPT_DATE = '2021-11-12';
my $OPT_TOOL = `which bash`;
chomp($OPT_TOOL);
my @PARAM = qw(--tool $OPT_TOOL --bool $OPT_BOOL --STR $OPT_STR
--INT $OPT_INT --REAL $OPT_REAL --DATE $OPT_DATE
--output OUTPUT --response ERROR);
# -----------------------------------------------------
# Command environment setup
# -----------------------------------------------------
# Command
my $COMMAND = 'command1';
# Command Parameter Brancher
my $COMMANDS = {
# Assoc. sub for the command
$COMMAND => [ \&testCommant,
# Parameter set for the sub
{ INT => $OPT_INT ,
STR => $OPT_STR ,
BOOL => $OPT_BOOL ,
DATE => $OPT_DATE ,
REAL => $OPT_REAL ,
TOOL => $OPT_TOOL },
],
};
# Create a new application object
$TK->new;
# Init Application Context
ok($TK->initApplication($VERSION, $DATE, $AUTHOR, $CONTACT))
...
# Init Runtime context before parsing the CLI data
ok($TK->initRuntime($MAGIC, $TOOL, $IOTYPE, \@PARAM),
"Init runtime set");
...
# Set runtime parameter before calling the command brancher
# to run a certein sub routine with a defined parameter set.
ok($TK->setRuntime($COMMANDS, $OUTPUT, $RES_ERROR, $COMMAND));
setRuntimeexitFunc 根據變數 $RESPONSE設定 $TK internal an 。
$RESPONSE = 'ERROR'鏈接方法$exitFun = \&exitError is $TK->exitError。$RESPONSE = 'WARN'鏈接方法$exitFun = \&exitWarn is $TK->exitWarn。
在測驗套件中,我想測驗哪個$exitFun(代碼參考)由$TK->getRuntimeValues.
# Check what was stored
my ( $tool, $output, $response,
$command, $exitFun, $param ) = $TK->getRuntimeValues;
ok($tool eq $TOOL); # OK
ok($output eq $OUTPUT); # OK
ok($response eq $RES_ERROR); # OK
ok($command eq $COMMAND);
...
# Struggle point
ok($exitFun == \$TK->exitError);
diag($exitFun,"\n"); # OK .. CODE(0x55a8a9027808)
diag(\$TK->exitError,"\n"); # ERROR .. Too few args for AppTK::exitError
似乎測驗試圖呼叫該方法。測驗從方法以外的方法獲取 CODE REF 的正確運算式是什么$TK->?
編輯定義:
在包中AppTK,關系由字典定義:
my %EXITS = (
FATAL => \&exitFatal, # Response that signals a failure
WARN => \&exitWarn, # Response that signals a warning
ERROR => \&exitError, # Response that signals a Error
INFO => \&exitInfo, # Response that signals a Info
);
...
# -----------------------------------------------------------------
sub exitRuntime ( $self, $error, $remarks = '', $example = '' ) {
my $exitFun = $self->runtime->{ CONST::KEY_EXIT_FUN };
&$exitFun( $self, $error, $remarks, $example );
}
# -----------------------------------------------------------------
sub exitError ( $self, $message, $remarks = '', $example = '' ) {
exitStatus( $self, 'E', $message, $remarks, $example );
}
# -----------------------------------------------------------------
sub exitFatal ( $self, $message, $remarks = '', $example = '' ) {
exitStatus( $self, 'F', $message, $remarks, $example );
}
# -----------------------------------------------------------------
sub exitInfo ( $self, $message, $remarks = '', $example = '' ) {
exitStatus( $self, 'I', $message, $remarks, $example );
}
# -----------------------------------------------------------------
sub exitCancel ( $self, $message, $remarks = '', $example = '' ) {
exitStatus( $self, 'C', $message, $remarks, $example );
}
# -----------------------------------------------------------------
sub exitWarn ( $self, $message, $remarks = '', $example = '' ) {
exitStatus( $self, 'W', $message, $remarks, $example );
}
uj5u.com熱心網友回復:
您在AppTK包中有一個名為getRuntimeValues有時會回傳的子程式\&exitError,您想檢查它是否是這樣做的。
為此,您可以與以下內容進行比較:
\&AppTK::exitError # The sub returned by `\&exitError` from package `AppTK`
雖然不是嚴格等價的,但如果$TK被祝福的話,以下將產生相同的結果AppTK:
$TK->can("exitError") # The sub called by `$TK->exitError`
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/405157.html
標籤:
下一篇:運行時Perl/Tk中的未知問題
