File: erl_format.3

package info (click to toggle)
erlang-manpages 1%3A12.b.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,188 kB
  • ctags: 2
  • sloc: makefile: 68; perl: 30; sh: 15
file content (105 lines) | stat: -rw-r--r-- 2,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
.TH erl_format 3 "erl_interface  3.5.7" "Ericsson AB" "C LIBRARY FUNCTIONS"
.SH NAME
erl_format \- Create and Match Erlang Terms
.SH DESCRIPTION
.LP
This module contains two routines - one general function for creating Erlang terms and one for pattern matching Erlang terms\&.

.SH EXPORTS
.LP
.B
ETERM * erl_format(FormatStr, \&.\&.\&. )
.br
.RS
.TP
Types
char *FormatStr;
.br
.RE
.RS
.LP
This is a general function for creating Erlang terms using a format specifier and a corresponding set of arguments, much in the way \fIprintf()\fR works\&.
.LP
\fIFormatStr\fR is a format specification string\&. The set of valid format specifiers is as follows:
.RS 2
.TP 2
*
~i - Integer
.TP 2
*
~f - Floating point
.TP 2
*
~a - Atom
.TP 2
*
~s - String
.TP 2
*
~w - Arbitrary Erlang term
.RE
.LP
For each format specifier that appears in \fIFormatStr\fR, there must be a corresponding argument following \fIFormatStr\fR\&. An Erlang term is built according to the \fIFormatStr\fR with values and Erlang terms substituted from the corresponding arguments and according to the individual format specifiers\&. For example:

.nf
erl_format("[{name,~a},{age,~i},{data,~w}]",
           "madonna",
           21,
           erl_format("[{adr,~s,~i}]","E-street",42));
        
.fi
.LP
This will create an \fI(ETERM *)\fR structure corresponding to the Erlang term: \fI[{name, madonna}, {age, 21}, {data, [{adr, "E-street", 42}]}]\fR
.LP
The function returns an Erlang term, or NULL if \fIFormatStr\fR does not describe a valid Erlang term\&.
.RE
.LP
.B
int erl_match(Pattern, Term)
.br
.RS
.TP
Types
ETERM *Pattern, *Term;
.br
.RE
.RS
.LP
This function is used to perform pattern matching similar to that done in Erlang\&. Refer to an Erlang manual for matching rules and more examples\&.
.LP
\fIPattern\fR is an Erlang term, possibly containing unbound variables\&. 
.LP
\fITerm\fR is an Erlang term that we wish to match against \fIPattern\fR\&.
.LP
\fITerm\fR and \fIPattern\fR are compared, and any unbound variables in \fIPattern\fR are bound to corresponding values in \fITerm\fR\&. 
.LP
If \fITerm\fR and \fIPattern\fR can be matched, the function returns a non-zero value and binds any unbound variables in \fIPattern\fR\&. If \fITerm\fR\fIPattern\fR do not match, the function returns 0\&. For example:

.nf
ETERM *term, *pattern, *pattern2;
term1    = erl_format("{14,21}");
term2    = erl_format("{19,19}");
pattern1 = erl_format("{A,B}");
pattern2 = erl_format("{F,F}");
if (erl_match(pattern1, term1)) {
  /* match succeeds:
   * A gets bound to 14, 
   * B gets bound to 21 
   */
  \&.\&.\&.  
}
if (erl_match(pattern2, term1)) {
  /* match fails because F cannot be 
   * bound to two separate values, 14 and 21
   */
  \&.\&.\&.
}
if (erl_match(pattern2, term2)) {
  /* match succeeds and F gets bound to 19 */
  \&.\&.\&.
}
        
.fi
.LP
\fIerl_var_content()\fR can be used to retrieve the content of any variables bound as a result of a call to \fIerl_match()\fR\&.
.RE