Erlang:变量未绑定 [英] Erlang: variable is unbound

查看:81
本文介绍了Erlang:变量未绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下为什么说变量不受限制?

Why is the following saying variable unbound?

9> {<<A:Length/binary, Rest/binary>>, Length} = {<<1,2,3,4,5>>, 3}.     
* 1: variable 'Length' is unbound

很明显,长度应该为3。

我正在尝试使用类似模式匹配的函数,即:

I am trying to have a function with similar pattern matching, ie.:

parse(<<Body:Length/binary, Rest/binary>>, Length) ->

但是如果由于相同的原因失败。如何实现我想要的模式匹配?

But if fails with the same reason. How can I achieve the pattern matching I want?

我真正想要实现的是解析传入的TCP流数据包

What I am really trying to achieve is parse in incoming tcp stream packets as LTV(Length, Type, Value).

在我解析了长度和类型之后的某个时候,我只想准备最多 Length 个字节作为值,其余部分可能用于下一个LTV。

At some point after I parse the the Length and the Type, I want to ready only up to Length number of bytes as the value, as the rest will probably be for the next LTV.

所以我的 parse_value 函数是这样的:

So my parse_value function is like this:

parse_value(Value0, Left, Callback = {Module, Function},
       {length, Length, type, Type, value, Value1}) when byte_size(Value0) >= Left ->
    <<Value2:Left/binary, Rest/binary>> = Value0,
    Module:Function({length, Length, type, Type, value, lists:reverse([Value2 | Value1])}),
    if 
    Rest =:= <<>> ->
        {?MODULE, parse, {}};
    true ->
        parse(Rest, Callback, {})
    end;
parse_value(Value0, Left, _, {length, Length, type, Type, value, Value1}) ->
    {?MODULE, parse_value, Left - byte_size(Value0), {length, Length, type, Type, value, [Value0 | Value1]}}.

如果可以进行模式匹配,可以将其分解为更令人愉悦的外观。

If I could do the pattern matching, I could break it up to something more pleasant to the eye.

推荐答案

模式匹配的规则是,如果变量X出现在两个子模式中,例如{X,X},或者{X,[X]}或类似的字符,则它们在两个位置必须具有相同的值,但每个子模式的匹配仍在相同的输入环境中完成-一侧的绑定不会转移到另一侧。相等性检查将在概念上完成,就像您在{X,X2}上进行了匹配并添加了保护X =:= X2一样。这意味着即使没有将元组中的Length字段设为最左侧的元素,也不能将其用作二进制模式的输入。

The rules for pattern matching are that if a variable X occurs in two subpatterns, as in {X, X}, or {X, [X]}, or similar, then they have to have the same value in both positions, but the matching of each subpattern is still done in the same input environment - bindings from one side do not carry over to the other. The equality check is conceptually done afterwards, as if you had matched on {X, X2} and added a guard X =:= X2. This means that your Length field in the tuple cannot be used as input to the binary pattern, not even if you make it the leftmost element.

但是,是二进制模式,绑定在字段中的变量可以从左到右在其后的其他字段中使用。因此,以下工作(在二进制文件中使用前导32位大小的字段):

However, within a binary pattern, variables bound in a field can be used in other fields following it, left-to-right. Therefore, the following works (using a leading 32-bit size field in the binary):

1> <<Length:32, A:Length/binary, Rest/binary>> = <<0,0,0,3,1,2,3,4,5>>.
<<0,0,0,3,1,2,3,4,5>>
2> A.
<<1,2,3>>
3> Rest.                                                               
<<4,5>>

这篇关于Erlang:变量未绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆