PDK::Utils::Context - 配置解析上下文工具类
SYNOPSIS
use PDK::Utils::Context;
my $ctx = PDK::Utils::Context->new(config => [
"line1",
"line2",
"line3",
]);
say $ctx->confSign; # 打印配置签名(MD5)
say $ctx->timestamp; # 打印时间戳
# 逐行解析
while (my $line = $ctx->nextUnParsedLine) {
say "解析: $line";
}
# 回溯与忽略
$ctx->backtrack;
$ctx->ignore;
DESCRIPTION
该模块用于管理和解析配置内容,提供游标控制、行解析标志、签名生成、时间戳管理等功能,方便逐行读取和回溯。
ATTRIBUTES
config
isa => ArrayRef[Str]
is => 'ro'
required => 1
配置内容数组,每个元素代表一行配置。
content
isa => Str
is => 'ro'
lazy => 1
配置内容字符串,默认由 config
数组拼接生成。
cursor
isa => Int
is => 'ro'
default => 0
当前解析游标,指向配置中的当前位置。
confSign
is => 'ro'
lazy => 1
配置签名,基于配置内容的 MD5 哈希生成。
timestamp
is => 'ro'
lazy => 1
时间戳,使用 PDK::Utils::Date 生成的格式化日期时间字符串。
METHODS
goToHead
$ctx->goToHead();
游标回到配置的开头(位置 0)。
nextLine
my $line = $ctx->nextLine();
获取下一行配置,并将游标向前移动一行。若已到末尾则返回 undef。
prevLine
$ctx->prevLine();
游标回退一行,若已在开头则返回 undef 并产生警告。
nextUnParsedLine
my $line = $ctx->nextUnParsedLine();
获取下一个未解析的配置行,并将其标记为已解析。自动跳过空行或空白内容。 支持自动检测并尝试解码字符集。
backtrack
$ctx->backtrack();
回溯到上一个解析点,并将该行的解析标志清除。
ignore
$ctx->ignore();
忽略当前行,等价于 backtrack
然后 nextLine
。
getUnParsedLines
my $lines = $ctx->getUnParsedLines();
获取所有未解析的配置行,返回字符串。
getParseFlag
my $flag = $ctx->getParseFlag();
获取当前行的解析标志(0 表示未解析,1 表示已解析)。
setParseFlag
$ctx->setParseFlag(1);
设置当前行的解析标志,默认为 1。
ERROR HANDLING
解码失败时会抛出异常或产生警告。
游标越界时(如 prevLine / backtrack 在开头调用),会返回 undef 并产生警告。
EXAMPLES
use PDK::Utils::Context;
my $ctx = PDK::Utils::Context->new(config => [
"line1",
"line2",
"line3",
]);
say $ctx->confSign; # 打印配置签名(MD5)
say $ctx->timestamp; # 打印时间戳
# 逐行解析
while (my $line = $ctx->nextUnParsedLine) {
say "解析: $line";
}
# 回溯与忽略
$ctx->backtrack;
$ctx->ignore;
AUTHOR
WENWU YAN <968828@gmail.com>
LICENSE AND COPYRIGHT
This software is licensed under the same terms as Perl itself.