[erlang-questions] binary:compile_pattern - I'm very confused!

30 views
Skip to first unread message

Steve Strong

unread,
Feb 8, 2013, 5:09:07 PM2/8/13
to erlang-q...@erlang.org
Hi,

I've got a binary split that I'm doing fairly regularly, so I wanted to use a compiled pattern to make it more efficient.  I don't have any state floating around (it's very much a utility function), so my plan was to compile the pattern in an erl shell and just paste the resultant term directly into my source code (with an appropriate comment!).  However, things are not as they seem:

Erlang R15B02 (erts-5.9.2) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.2  (abort with ^G)
1> C = binary:compile_pattern(<<0,0,1,0>>).
{bm,<<>>}
2> X = <<2,3,4,0,0,1,0,5,6,7>>.
<<2,3,4,0,0,1,0,5,6,7>>
3> binary:split(X, C).
[<<2,3,4>>,<<5,6,7>>]
4> binary:split(X, {bm,<<>>}).
** exception error: bad argument
     in function  binary:split/3 (binary.erl, line 84)
5> D = binary_to_term(term_to_binary(C)).
{bm,<<>>}
6> C = D.
{bm,<<>>}
7> D =:= C.
true
8> binary:split(X, D).
** exception error: bad argument
     in function  binary:split/3 (binary.erl, line 84)
9> 

The above should be fairly self-explanatory - I'm very confused and either being particularly dim tonight, or there is some dark magic at work :)  Can anyone shed any light?

Cheers,

Steve

-- 
Steve Strong
Sent with Sparrow

Lukas Larsson

unread,
Feb 8, 2013, 5:36:02 PM2/8/13
to Steve Strong, Erlang Questions
There is dark magic at work. What you got there is what is referred to as a "magic" binary. It represents some internal state within the erlang emulator and is used by binary:compile_pattern and nifs. You will have to store it someplace else (like you state) and fetch it from there when you are doing the split. 


_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions


Steve Vinoski

unread,
Feb 8, 2013, 5:36:17 PM2/8/13
to Steve Strong, erlang-q...@erlang.org
On Fri, Feb 8, 2013 at 5:09 PM, Steve Strong <st...@srstrong.com> wrote:
Hi,

I've got a binary split that I'm doing fairly regularly, so I wanted to use a compiled pattern to make it more efficient.  I don't have any state floating around (it's very much a utility function), so my plan was to compile the pattern in an erl shell and just paste the resultant term directly into my source code (with an appropriate comment!).  However, things are not as they seem:

Erlang R15B02 (erts-5.9.2) [source] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.2  (abort with ^G)
1> C = binary:compile_pattern(<<0,0,1,0>>).
{bm,<<>>}

On the Erlang binary module man page (http://www.erlang.org/doc/man/binary.html) it describes the return value of compile_pattern as follows:

"Opaque data-type representing a compiled search-pattern. Guaranteed to be a tuple() to allow programs to distinguish it from non precompiled search patterns."

The binary:compile_pattern function is a BIF that creates a "magic binary", much like the resources one can create in a NIF, and these appear as empty binaries in Erlang code as you can see in your shell output, but they're really opaque data types. Because of this, your approach won't work because the subsequent functions you're calling think you're passing an actual empty binary instead of the required opaque type.

--steve

Steve Strong

unread,
Feb 11, 2013, 1:39:20 PM2/11/13
to Steve Vinoski, erlang-q...@erlang.org
All makes sense, although for know I'll continue do describe it as evilness ;)

Cheers,

Steve

-- 
Steve Strong
Sent with Sparrow

Reply all
Reply to author
Forward
0 new messages