warnings
[emacs.git] / lisp / org / ob-perl.el
blob13d7141373187bae312310f11fba270cbba2c2b9
1 ;;; ob-perl.el --- org-babel functions for perl evaluation
3 ;; Copyright (C) 2009-2011 Free Software Foundation
5 ;; Author: Dan Davison
6 ;; Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.7
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Org-Babel support for evaluating perl source code.
30 ;;; Code:
31 (require 'ob)
32 (require 'ob-eval)
33 (eval-when-compile (require 'cl))
35 (defvar org-babel-tangle-lang-exts)
36 (add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
38 (defvar org-babel-default-header-args:perl '())
40 (defvar org-babel-perl-command "perl"
41 "Name of command to use for executing perl code.")
43 (defun org-babel-execute:perl (body params)
44 "Execute a block of Perl code with Babel.
45 This function is called by `org-babel-execute-src-block'."
46 (let* ((session (cdr (assoc :session params)))
47 (result-params (cdr (assoc :result-params params)))
48 (result-type (cdr (assoc :result-type params)))
49 (full-body (org-babel-expand-body:generic
50 body params (org-babel-variable-assignments:perl params)))
51 (session (org-babel-perl-initiate-session session)))
52 (org-babel-reassemble-table
53 (org-babel-perl-evaluate session full-body result-type)
54 (org-babel-pick-name
55 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
56 (org-babel-pick-name
57 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
59 (defun org-babel-prep-session:perl (session params)
60 "Prepare SESSION according to the header arguments in PARAMS."
61 (error "Sessions are not supported for Perl."))
63 (defun org-babel-variable-assignments:perl (params)
64 "Return list of perl statements assigning the block's variables"
65 (mapcar
66 (lambda (pair)
67 (format "$%s=%s;"
68 (car pair)
69 (org-babel-perl-var-to-perl (cdr pair))))
70 (mapcar #'cdr (org-babel-get-header params :var))))
72 ;; helper functions
74 (defun org-babel-perl-var-to-perl (var)
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."
78 (if (listp var)
79 (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
80 (format "%S" var)))
82 (defvar org-babel-perl-buffers '(:default . nil))
84 (defun org-babel-perl-initiate-session (&optional session params)
85 "Return nil because sessions are not supported by perl"
86 nil)
88 (defvar org-babel-perl-wrapper-method
90 sub main {
93 @r = main;
94 open(o, \">%s\");
95 print o join(\"\\n\", @r), \"\\n\"")
97 (defvar org-babel-perl-pp-wrapper-method
98 nil)
100 (defun org-babel-perl-evaluate (session body &optional result-type)
101 "Pass BODY to the Perl process in SESSION.
102 If RESULT-TYPE equals 'output then return a list of the outputs
103 of the statements in BODY, if RESULT-TYPE equals 'value then
104 return the value of the last statement in BODY, as elisp."
105 (when session (error "Sessions are not supported for Perl."))
106 (case result-type
107 (output (org-babel-eval org-babel-perl-command body))
108 (value (let ((tmp-file (org-babel-temp-file "perl-")))
109 (org-babel-eval
110 org-babel-perl-command
111 (format org-babel-perl-wrapper-method body
112 (org-babel-process-file-name tmp-file 'noquote)))
113 (org-babel-eval-read-file tmp-file)))))
115 (provide 'ob-perl)
119 ;;; ob-perl.el ends here