Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-perl.el --- org-babel functions for perl evaluation |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2009-2014 Free Software Foundation, Inc. |
86fbb8ca | 4 | |
dfd98937 BG |
5 | ;; Authors: Dan Davison |
6 | ;; Eric Schulte | |
86fbb8ca CD |
7 | ;; Keywords: literate programming, reproducible research |
8 | ;; Homepage: http://orgmode.org | |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Org-Babel support for evaluating perl source code. | |
28 | ||
29 | ;;; Code: | |
30 | (require 'ob) | |
86fbb8ca CD |
31 | (eval-when-compile (require 'cl)) |
32 | ||
3ab2c837 | 33 | (defvar org-babel-tangle-lang-exts) |
86fbb8ca CD |
34 | (add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl")) |
35 | ||
36 | (defvar org-babel-default-header-args:perl '()) | |
37 | ||
38 | (defvar org-babel-perl-command "perl" | |
39 | "Name of command to use for executing perl code.") | |
40 | ||
86fbb8ca CD |
41 | (defun org-babel-execute:perl (body params) |
42 | "Execute a block of Perl code with Babel. | |
43 | This function is called by `org-babel-execute-src-block'." | |
afe98dfa CD |
44 | (let* ((session (cdr (assoc :session params))) |
45 | (result-params (cdr (assoc :result-params params))) | |
46 | (result-type (cdr (assoc :result-type params))) | |
47 | (full-body (org-babel-expand-body:generic | |
48 | body params (org-babel-variable-assignments:perl params))) | |
8223b1d2 | 49 | (session (org-babel-perl-initiate-session session))) |
86fbb8ca | 50 | (org-babel-reassemble-table |
271672fa | 51 | (org-babel-perl-evaluate session full-body result-type result-params) |
86fbb8ca | 52 | (org-babel-pick-name |
afe98dfa | 53 | (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) |
86fbb8ca | 54 | (org-babel-pick-name |
afe98dfa | 55 | (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))) |
86fbb8ca CD |
56 | |
57 | (defun org-babel-prep-session:perl (session params) | |
58 | "Prepare SESSION according to the header arguments in PARAMS." | |
8223b1d2 | 59 | (error "Sessions are not supported for Perl")) |
86fbb8ca | 60 | |
afe98dfa | 61 | (defun org-babel-variable-assignments:perl (params) |
8223b1d2 | 62 | "Return list of perl statements assigning the block's variables." |
afe98dfa CD |
63 | (mapcar |
64 | (lambda (pair) | |
271672fa | 65 | (org-babel-perl--var-to-perl (cdr pair) (car pair))) |
afe98dfa CD |
66 | (mapcar #'cdr (org-babel-get-header params :var)))) |
67 | ||
86fbb8ca CD |
68 | ;; helper functions |
69 | ||
271672fa BG |
70 | (defvar org-babel-perl-var-wrap "q(%s)" |
71 | "Wrapper for variables inserted into Perl code.") | |
72 | ||
73 | (defvar org-babel-perl--lvl) | |
74 | (defun org-babel-perl--var-to-perl (var &optional varn) | |
86fbb8ca CD |
75 | "Convert an elisp value to a perl variable. |
76 | The elisp value, VAR, is converted to a string of perl source code | |
77 | specifying a var of the same value." | |
271672fa BG |
78 | (if varn |
79 | (let ((org-babel-perl--lvl 0) (lvar (listp var)) prefix) | |
80 | (concat "my $" (symbol-name varn) "=" (when lvar "\n") | |
81 | (org-babel-perl--var-to-perl var) | |
82 | ";\n")) | |
83 | (let ((prefix (make-string (* 2 org-babel-perl--lvl) ?\ ))) | |
84 | (concat prefix | |
85 | (if (listp var) | |
86 | (let ((org-babel-perl--lvl (1+ org-babel-perl--lvl))) | |
87 | (concat "[\n" | |
88 | (mapconcat #'org-babel-perl--var-to-perl var "") | |
89 | prefix "]")) | |
90 | (format "q(%s)" var)) | |
91 | (unless (zerop org-babel-perl--lvl) ",\n"))))) | |
86fbb8ca CD |
92 | |
93 | (defvar org-babel-perl-buffers '(:default . nil)) | |
94 | ||
95 | (defun org-babel-perl-initiate-session (&optional session params) | |
8223b1d2 BG |
96 | "Return nil because sessions are not supported by perl." |
97 | nil) | |
86fbb8ca | 98 | |
271672fa BG |
99 | (defvar org-babel-perl-wrapper-method "{ |
100 | my $babel_sub = sub { | |
101 | %s | |
102 | }; | |
103 | open my $BOH, qq(>%s) or die qq(Perl: Could not open output file.$/); | |
104 | my $rv = &$babel_sub(); | |
105 | my $rt = ref $rv; | |
106 | select $BOH; | |
107 | if (qq(ARRAY) eq $rt) { | |
108 | local $\\=$/; | |
109 | local $,=qq(\t); | |
110 | foreach my $rv ( @$rv ) { | |
111 | my $rt = ref $rv; | |
112 | if (qq(ARRAY) eq $rt) { | |
113 | print @$rv; | |
114 | } else { | |
115 | print $rv; | |
116 | } | |
117 | } | |
118 | } else { | |
119 | print $rv; | |
120 | } | |
121 | }") | |
122 | ||
123 | (defvar org-babel-perl-preface nil) | |
86fbb8ca CD |
124 | |
125 | (defvar org-babel-perl-pp-wrapper-method | |
126 | nil) | |
127 | ||
271672fa | 128 | (defun org-babel-perl-evaluate (session ibody &optional result-type result-params) |
86fbb8ca CD |
129 | "Pass BODY to the Perl process in SESSION. |
130 | If RESULT-TYPE equals 'output then return a list of the outputs | |
131 | of the statements in BODY, if RESULT-TYPE equals 'value then | |
132 | return the value of the last statement in BODY, as elisp." | |
8223b1d2 | 133 | (when session (error "Sessions are not supported for Perl")) |
271672fa BG |
134 | (let* ((body (concat org-babel-perl-preface ibody)) |
135 | (tmp-file (org-babel-temp-file "perl-")) | |
136 | (tmp-babel-file (org-babel-process-file-name | |
137 | tmp-file 'noquote))) | |
666ffc7e SM |
138 | (let ((results |
139 | (case result-type | |
140 | (output | |
141 | (with-temp-file tmp-file | |
142 | (insert | |
143 | (org-babel-eval org-babel-perl-command body)) | |
144 | (buffer-string))) | |
145 | (value | |
146 | (org-babel-eval org-babel-perl-command | |
147 | (format org-babel-perl-wrapper-method | |
148 | body tmp-babel-file)))))) | |
149 | (when results | |
150 | (org-babel-result-cond result-params | |
151 | (org-babel-eval-read-file tmp-file) | |
152 | (org-babel-import-elisp-from-file tmp-file '(16))))))) | |
86fbb8ca CD |
153 | |
154 | (provide 'ob-perl) | |
155 | ||
5b409b39 | 156 | |
86fbb8ca CD |
157 | |
158 | ;;; ob-perl.el ends here |