简体   繁体   English

获取 Perl 打包/解包模板的元素数

[英]Get the number of elements for a Perl pack/unpack template

Given a template string for the pack or unpack function, is there a simple way to determine the minimum number of additional arguments expected by pack , and/or the minimum number of values that would be returned by a successful unpack ?给定packunpack函数的模板字符串,是否有一种简单的方法来确定pack预期的附加参数的最小数量,和/或成功unpack将返回的值的最小数量?

For example:例如:

unpack_element_count("vVC16a16") => 19 # a16 gives one string
pack_element_count("(VVVvx4)2")  => 8  # x4 ignored, 4*2
pack_element_count("v*")         => 0

$count =()= unpack($template, ""); doesn't sound good, since the documentation for unpack says on attempting to unpack a string which is too short, "the result is not well defined".听起来不太好,因为unpack的文档说在尝试解压太短的字符串时,“结果定义不明确”。 I suppose I could pass a string with an enormous number of zero bytes, but that sounds rather inefficient, and possibly might not be valid for some fancier templates?我想我可以传递一个包含大量零字节的字符串,但这听起来效率很低,并且可能对某些更高级的模板无效?

If it helps, I'm mainly actually interested in just templates with fixed element counts, meaning no * counts, no / prefix-count-in-data formats, etc. So an answer to this limited case will be helpful;如果有帮助,我实际上主要只对具有固定元素计数的模板感兴趣,这意味着没有*计数、没有/前缀计数数据格式等。因此,对这种有限情况的回答会有所帮助; an answer to the more general case (the minimum of a variable count) would be interesting.对更一般情况(变量计数的最小值)的回答会很有趣。

There's no builtin tool provided to do this.没有提供内置工具来执行此操作。

I doubt something exists on CPAN, but feel free to search there to see if there's something that can help you.我怀疑 CPAN 上是否存在某些内容,但请随时在那里搜索,看看是否有可以帮助您的内容。 (Module recommendations are off-topic.) (模块推荐是题外话。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM