Merge branch 'master' of orgmode.org:org-mode
[org-mode/org-jambu.git] / lisp / ob-perl.el
blob1e0cecb1af0d10f8e53d678cd056e1bfea37defa
1 ;;; ob-perl.el --- org-babel functions for perl evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation
5 ;; Author: Dan Davison, Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.4
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating perl source code.
29 ;;; Code:
30 (require 'ob)
31 (require 'ob-eval)
32 (eval-when-compile (require 'cl))
34 (add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
36 (defvar org-babel-default-header-args:perl '())
38 (defvar org-babel-perl-command "perl"
39 "Name of command to use for executing perl code.")
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'."
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)))
49 (session (org-babel-perl-initiate-session session)))
50 (org-babel-reassemble-table
51 (org-babel-perl-evaluate session full-body result-type)
52 (org-babel-pick-name
53 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
54 (org-babel-pick-name
55 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
57 (defun org-babel-prep-session:perl (session params)
58 "Prepare SESSION according to the header arguments in PARAMS."
59 (error "Sessions are not supported for Perl."))
61 (defun org-babel-variable-assignments:perl (params)
62 "Return list of perl statements assigning the block's variables"
63 (mapcar
64 (lambda (pair)
65 (format "$%s=%s;"
66 (car pair)
67 (org-babel-perl-var-to-perl (cdr pair))))
68 (mapcar #'cdr (org-babel-get-header params :var))))
70 ;; helper functions
72 (defun org-babel-perl-var-to-perl (var)
73 "Convert an elisp value to a perl variable.
74 The elisp value, VAR, is converted to a string of perl source code
75 specifying a var of the same value."
76 (if (listp var)
77 (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
78 (format "%S" var)))
80 (defvar org-babel-perl-buffers '(:default . nil))
82 (defun org-babel-perl-initiate-session (&optional session params)
83 "Return nil because sessions are not supported by perl"
84 nil)
86 (defvar org-babel-perl-wrapper-method
88 sub main {
91 @r = main;
92 open(o, \">%s\");
93 print o join(\"\\n\", @r), \"\\n\"")
95 (defvar org-babel-perl-pp-wrapper-method
96 nil)
98 (defun org-babel-perl-evaluate (session body &optional result-type)
99 "Pass BODY to the Perl process in SESSION.
100 If RESULT-TYPE equals 'output then return a list of the outputs
101 of the statements in BODY, if RESULT-TYPE equals 'value then
102 return the value of the last statement in BODY, as elisp."
103 (when session (error "Sessions are not supported for Perl."))
104 (case result-type
105 (output (org-babel-eval org-babel-perl-command body))
106 (value (let ((tmp-file (org-babel-temp-file "perl-")))
107 (org-babel-eval
108 org-babel-perl-command
109 (format org-babel-perl-wrapper-method body
110 (org-babel-process-file-name tmp-file 'noquote)))
111 (org-babel-eval-read-file tmp-file)))))
113 (provide 'ob-perl)
115 ;; arch-tag: 88ef9396-d857-4dc3-8946-5a72bdfa2337
117 ;;; ob-perl.el ends here