2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-perl.el --- org-babel functions for perl evaluation
|
2009-11-07 03:05:58 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; Copyright (C) 2009, 2010 Free Software Foundation
|
2009-11-07 03:05:58 +00:00
|
|
|
|
|
|
|
;; Author: Dan Davison, Eric Schulte
|
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
2010-07-19 06:33:24 +00:00
|
|
|
;; Version: 7.01trans
|
2009-11-07 03:05:58 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-11-07 03:05:58 +00:00
|
|
|
|
2010-06-25 16:32:35 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-11-07 03:05:58 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 16:32:35 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-11-07 03:05:58 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
2010-06-25 16:32:35 +00:00
|
|
|
|
2009-11-07 03:05:58 +00:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2010-06-25 16:32:35 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2009-11-07 03:05:58 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Org-Babel support for evaluating perl source code.
|
|
|
|
|
|
|
|
;;; Code:
|
2010-06-11 23:02:42 +00:00
|
|
|
(require 'ob)
|
2010-07-04 21:46:13 +00:00
|
|
|
(require 'ob-eval)
|
2010-07-04 20:38:24 +00:00
|
|
|
(eval-when-compile (require 'cl))
|
2009-11-07 03:05:58 +00:00
|
|
|
|
2010-06-17 17:21:14 +00:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
|
|
|
|
|
2010-06-25 17:12:54 +00:00
|
|
|
(defvar org-babel-default-header-args:perl '())
|
|
|
|
|
2010-07-04 20:38:24 +00:00
|
|
|
(defvar org-babel-perl-command "perl"
|
|
|
|
"Name of command to use for executing perl code.")
|
|
|
|
|
2010-04-16 05:12:02 +00:00
|
|
|
(defun org-babel-expand-body:perl (body params &optional processed-params)
|
2010-06-12 00:12:15 +00:00
|
|
|
"Expand BODY according to PARAMS, return the expanded body."
|
2010-06-25 17:26:48 +00:00
|
|
|
(let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
|
2010-04-16 05:12:02 +00:00
|
|
|
(concat
|
|
|
|
(mapconcat ;; define any variables
|
|
|
|
(lambda (pair)
|
|
|
|
(format "$%s=%s;"
|
|
|
|
(car pair)
|
|
|
|
(org-babel-perl-var-to-perl (cdr pair))))
|
|
|
|
vars "\n") "\n" (org-babel-trim body) "\n")))
|
|
|
|
|
2009-11-07 03:05:58 +00:00
|
|
|
(defun org-babel-execute:perl (body params)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Execute a block of Perl code with Babel.
|
|
|
|
This function is called by `org-babel-execute-src-block'."
|
2009-11-10 22:22:25 +00:00
|
|
|
(let* ((processed-params (org-babel-process-params params))
|
2010-07-03 02:21:31 +00:00
|
|
|
(session (nth 0 processed-params))
|
2010-06-25 17:26:48 +00:00
|
|
|
(vars (nth 1 processed-params))
|
|
|
|
(result-params (nth 2 processed-params))
|
|
|
|
(result-type (nth 3 processed-params))
|
2010-04-16 05:12:02 +00:00
|
|
|
(full-body (org-babel-expand-body:perl
|
2010-07-04 20:38:24 +00:00
|
|
|
body params processed-params))
|
2009-11-07 03:05:58 +00:00
|
|
|
(session (org-babel-perl-initiate-session session)))
|
2010-04-19 01:06:34 +00:00
|
|
|
(org-babel-reassemble-table
|
|
|
|
(org-babel-perl-evaluate session full-body result-type)
|
2010-07-04 20:38:24 +00:00
|
|
|
(org-babel-pick-name
|
|
|
|
(nth 4 processed-params) (cdr (assoc :colnames params)))
|
|
|
|
(org-babel-pick-name
|
|
|
|
(nth 5 processed-params) (cdr (assoc :rownames params))))))
|
2009-11-07 03:05:58 +00:00
|
|
|
|
|
|
|
(defun org-babel-prep-session:perl (session params)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Prepare SESSION according to the header arguments in PARAMS."
|
2009-11-07 03:05:58 +00:00
|
|
|
(error "Sessions are not supported for Perl."))
|
|
|
|
|
|
|
|
;; helper functions
|
|
|
|
|
|
|
|
(defun org-babel-perl-var-to-perl (var)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Convert an elisp value to a perl variable.
|
|
|
|
The elisp value, VAR, is converted to a string of perl source code
|
2009-11-07 03:05:58 +00:00
|
|
|
specifying a var of the same value."
|
|
|
|
(if (listp var)
|
|
|
|
(concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
|
|
|
|
(format "%S" var)))
|
|
|
|
|
|
|
|
(defvar org-babel-perl-buffers '(:default . nil))
|
|
|
|
|
2010-03-13 17:59:41 +00:00
|
|
|
(defun org-babel-perl-initiate-session (&optional session params)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Return nil because sessions are not supported by perl"
|
2009-11-07 03:05:58 +00:00
|
|
|
nil)
|
|
|
|
|
|
|
|
(defvar org-babel-perl-wrapper-method
|
|
|
|
"
|
|
|
|
sub main {
|
|
|
|
%s
|
|
|
|
}
|
|
|
|
@r = main;
|
|
|
|
open(o, \">%s\");
|
|
|
|
print o join(\"\\n\", @r), \"\\n\"")
|
|
|
|
|
|
|
|
(defvar org-babel-perl-pp-wrapper-method
|
|
|
|
nil)
|
|
|
|
|
|
|
|
(defun org-babel-perl-evaluate (session body &optional result-type)
|
2010-07-13 23:20:08 +00:00
|
|
|
"Pass BODY to the Perl process in SESSION.
|
|
|
|
If RESULT-TYPE equals 'output then return a list of the outputs
|
|
|
|
of the statements in BODY, if RESULT-TYPE equals 'value then
|
|
|
|
return the value of the last statement in BODY, as elisp."
|
2010-07-04 20:38:24 +00:00
|
|
|
(when session (error "Sessions are not supported for Perl."))
|
|
|
|
(case result-type
|
|
|
|
(output (org-babel-eval org-babel-perl-command body))
|
2010-08-25 20:43:07 +00:00
|
|
|
(value (let ((tmp-file (org-babel-temp-file "perl-results-")))
|
2010-07-04 20:38:24 +00:00
|
|
|
(org-babel-eval
|
2010-07-04 21:46:13 +00:00
|
|
|
org-babel-perl-command
|
2010-07-04 20:38:24 +00:00
|
|
|
(format org-babel-perl-wrapper-method body tmp-file))
|
|
|
|
(org-babel-eval-read-file tmp-file)))))
|
2009-11-07 03:05:58 +00:00
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
(provide 'ob-perl)
|
2010-06-25 16:32:35 +00:00
|
|
|
|
|
|
|
;; arch-tag: 88ef9396-d857-4dc3-8946-5a72bdfa2337
|
|
|
|
|
2010-06-11 23:02:42 +00:00
|
|
|
;;; ob-perl.el ends here
|