Merge from emacs-24; up to 2012-12-26T22:30:58Z!yamaoka@jpl.org
[bpt/emacs.git] / lisp / org / ob-perl.el
CommitLineData
86fbb8ca
CD
1;;; ob-perl.el --- org-babel functions for perl evaluation
2
ab422c4d 3;; Copyright (C) 2009-2013 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)
31(require 'ob-eval)
32(eval-when-compile (require 'cl))
33
3ab2c837 34(defvar org-babel-tangle-lang-exts)
86fbb8ca
CD
35(add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
36
37(defvar org-babel-default-header-args:perl '())
38
39(defvar org-babel-perl-command "perl"
40 "Name of command to use for executing perl code.")
41
86fbb8ca
CD
42(defun org-babel-execute:perl (body params)
43 "Execute a block of Perl code with Babel.
44This function is called by `org-babel-execute-src-block'."
afe98dfa
CD
45 (let* ((session (cdr (assoc :session params)))
46 (result-params (cdr (assoc :result-params params)))
47 (result-type (cdr (assoc :result-type params)))
48 (full-body (org-babel-expand-body:generic
49 body params (org-babel-variable-assignments:perl params)))
8223b1d2 50 (session (org-babel-perl-initiate-session session)))
86fbb8ca
CD
51 (org-babel-reassemble-table
52 (org-babel-perl-evaluate session full-body result-type)
53 (org-babel-pick-name
afe98dfa 54 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
86fbb8ca 55 (org-babel-pick-name
afe98dfa 56 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
86fbb8ca
CD
57
58(defun org-babel-prep-session:perl (session params)
59 "Prepare SESSION according to the header arguments in PARAMS."
8223b1d2 60 (error "Sessions are not supported for Perl"))
86fbb8ca 61
afe98dfa 62(defun org-babel-variable-assignments:perl (params)
8223b1d2 63 "Return list of perl statements assigning the block's variables."
afe98dfa
CD
64 (mapcar
65 (lambda (pair)
66 (format "$%s=%s;"
67 (car pair)
68 (org-babel-perl-var-to-perl (cdr pair))))
69 (mapcar #'cdr (org-babel-get-header params :var))))
70
86fbb8ca
CD
71;; helper functions
72
73(defun org-babel-perl-var-to-perl (var)
74 "Convert an elisp value to a perl variable.
75The elisp value, VAR, is converted to a string of perl source code
76specifying a var of the same value."
77 (if (listp var)
78 (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
79 (format "%S" var)))
80
81(defvar org-babel-perl-buffers '(:default . nil))
82
83(defun org-babel-perl-initiate-session (&optional session params)
8223b1d2
BG
84 "Return nil because sessions are not supported by perl."
85 nil)
86fbb8ca
CD
86
87(defvar org-babel-perl-wrapper-method
88 "
89sub main {
90%s
91}
92@r = main;
93open(o, \">%s\");
94print o join(\"\\n\", @r), \"\\n\"")
95
96(defvar org-babel-perl-pp-wrapper-method
97 nil)
98
99(defun org-babel-perl-evaluate (session body &optional result-type)
100 "Pass BODY to the Perl process in SESSION.
101If RESULT-TYPE equals 'output then return a list of the outputs
102of the statements in BODY, if RESULT-TYPE equals 'value then
103return the value of the last statement in BODY, as elisp."
8223b1d2 104 (when session (error "Sessions are not supported for Perl"))
86fbb8ca
CD
105 (case result-type
106 (output (org-babel-eval org-babel-perl-command body))
afe98dfa 107 (value (let ((tmp-file (org-babel-temp-file "perl-")))
86fbb8ca
CD
108 (org-babel-eval
109 org-babel-perl-command
afe98dfa
CD
110 (format org-babel-perl-wrapper-method body
111 (org-babel-process-file-name tmp-file 'noquote)))
86fbb8ca
CD
112 (org-babel-eval-read-file tmp-file)))))
113
114(provide 'ob-perl)
115
5b409b39 116
86fbb8ca
CD
117
118;;; ob-perl.el ends here