forked from bradfitz/campher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
59 lines (44 loc) · 1.01 KB
/
README
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
What do you get when you cross a Camel with a Gopher?
Campher!
Perl in Go.
Example of the awesomeness / horror:
=========
INSTALL
=========
$ go get github.com/bradfitz/campher
=========
EXAMPLE
=========
package main
import (
"fmt"
"strings"
"github.com/bradfitz/campher/perl"
)
func main() {
p := perl.NewInterpreter()
p.Eval(`$foo = "bar";`)
fmt.Println("foo is:", p.EvalString("$foo"))
cv := p.Eval(`sub { my ($a1, $a2, $func) = @_; $func->($a1, $a2); }`).CV()
var ret *SV
p := perl.NewInterpreter()
foo := p.Eval(`$foo = sub {
my ($op, $v1, $v2) = @_;
return "Perl says: " . $op->($v1, $v2);
};`)
concat := func(a, b string) string { return a + b }
fmt.Println("concat:", foo.CV().Call(concat, "foo", "bar"))
base := 0
add := func(a, b int) int {
base++
return a + b + base
}
fmt.Println("add:", foo.CV().Call(add, 1, "40"))
fmt.Println("add:", foo.CV().Call(add, 1, "40"))
}
================
EXAMPLE OUTPUT
================
concat: Perl says: foobar
add: Perl says: 42
add: Perl says: 43