2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-perl.el --- org-babel functions for perl evaluation
|
2009-11-06 22:05:58 -05:00
|
|
|
|
2012-01-05 03:43:21 -05:00
|
|
|
;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
|
2009-11-06 22:05:58 -05:00
|
|
|
|
2012-04-01 18:53:28 -04:00
|
|
|
;; Authors: Dan Davison
|
|
|
|
;; Eric Schulte
|
2009-11-06 22:05:58 -05:00
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
2009-11-06 22:05:58 -05:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-11-06 22:05:58 -05:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2010-06-25 12:32:35 -04: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-06 22:05:58 -05: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 12:32:35 -04:00
|
|
|
|
2009-11-06 22:05:58 -05:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
2010-06-25 12:32:35 -04:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2009-11-06 22:05:58 -05:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Org-Babel support for evaluating perl source code.
|
|
|
|
|
|
|
|
;;; Code:
|
2010-06-11 19:02:42 -04:00
|
|
|
(require 'ob)
|
2010-07-04 16:38:24 -04:00
|
|
|
(eval-when-compile (require 'cl))
|
2009-11-06 22:05:58 -05:00
|
|
|
|
2011-06-21 18:34:41 -04:00
|
|
|
(defvar org-babel-tangle-lang-exts)
|
2010-06-17 13:21:14 -04:00
|
|
|
(add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
|
|
|
|
|
2010-06-25 13:12:54 -04:00
|
|
|
(defvar org-babel-default-header-args:perl '())
|
|
|
|
|
2010-07-04 16:38:24 -04:00
|
|
|
(defvar org-babel-perl-command "perl"
|
|
|
|
"Name of command to use for executing perl code.")
|
|
|
|
|
2009-11-06 22:05:58 -05:00
|
|
|
(defun org-babel-execute:perl (body params)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Execute a block of Perl code with Babel.
|
|
|
|
This function is called by `org-babel-execute-src-block'."
|
2010-10-21 07:52:24 -04:00
|
|
|
(let* ((session (cdr (assoc :session params)))
|
|
|
|
(result-params (cdr (assoc :result-params params)))
|
|
|
|
(result-type (cdr (assoc :result-type params)))
|
2010-10-21 07:26:06 -04:00
|
|
|
(full-body (org-babel-expand-body:generic
|
|
|
|
body params (org-babel-variable-assignments:perl params)))
|
2012-08-11 13:10:44 -04:00
|
|
|
(session (org-babel-perl-initiate-session session)))
|
2010-04-18 21:06:34 -04:00
|
|
|
(org-babel-reassemble-table
|
|
|
|
(org-babel-perl-evaluate session full-body result-type)
|
2010-07-04 16:38:24 -04:00
|
|
|
(org-babel-pick-name
|
2010-10-21 07:52:24 -04:00
|
|
|
(cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
|
2010-07-04 16:38:24 -04:00
|
|
|
(org-babel-pick-name
|
2010-10-21 07:52:24 -04:00
|
|
|
(cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
|
2009-11-06 22:05:58 -05:00
|
|
|
|
|
|
|
(defun org-babel-prep-session:perl (session params)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Prepare SESSION according to the header arguments in PARAMS."
|
2012-09-28 11:47:48 -04:00
|
|
|
(error "Sessions are not supported for Perl"))
|
2009-11-06 22:05:58 -05:00
|
|
|
|
2010-10-21 07:26:06 -04:00
|
|
|
(defun org-babel-variable-assignments:perl (params)
|
2012-07-30 04:08:15 -04:00
|
|
|
"Return list of perl statements assigning the block's variables."
|
2010-10-21 07:26:06 -04:00
|
|
|
(mapcar
|
|
|
|
(lambda (pair)
|
|
|
|
(format "$%s=%s;"
|
|
|
|
(car pair)
|
|
|
|
(org-babel-perl-var-to-perl (cdr pair))))
|
|
|
|
(mapcar #'cdr (org-babel-get-header params :var))))
|
|
|
|
|
2009-11-06 22:05:58 -05:00
|
|
|
;; helper functions
|
|
|
|
|
|
|
|
(defun org-babel-perl-var-to-perl (var)
|
2010-07-13 19:20:08 -04:00
|
|
|
"Convert an elisp value to a perl variable.
|
|
|
|
The elisp value, VAR, is converted to a string of perl source code
|
2009-11-06 22:05:58 -05: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 12:59:41 -05:00
|
|
|
(defun org-babel-perl-initiate-session (&optional session params)
|
2012-07-30 04:08:15 -04:00
|
|
|
"Return nil because sessions are not supported by perl."
|
2012-08-11 13:10:44 -04:00
|
|
|
nil)
|
2009-11-06 22:05:58 -05:00
|
|
|
|
|
|
|
(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 19:20:08 -04: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."
|
2012-09-28 11:47:48 -04:00
|
|
|
(when session (error "Sessions are not supported for Perl"))
|
2010-07-04 16:38:24 -04:00
|
|
|
(case result-type
|
|
|
|
(output (org-babel-eval org-babel-perl-command body))
|
2010-09-20 19:38:31 -04:00
|
|
|
(value (let ((tmp-file (org-babel-temp-file "perl-")))
|
2010-07-04 16:38:24 -04:00
|
|
|
(org-babel-eval
|
2010-07-04 17:46:13 -04:00
|
|
|
org-babel-perl-command
|
2010-09-22 16:40:14 -04:00
|
|
|
(format org-babel-perl-wrapper-method body
|
|
|
|
(org-babel-process-file-name tmp-file 'noquote)))
|
2010-07-04 16:38:24 -04:00
|
|
|
(org-babel-eval-read-file tmp-file)))))
|
2009-11-06 22:05:58 -05:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
(provide 'ob-perl)
|
2010-06-25 12:32:35 -04:00
|
|
|
|
2011-08-15 14:04:38 -04:00
|
|
|
|
2010-06-25 12:32:35 -04:00
|
|
|
|
2010-06-11 19:02:42 -04:00
|
|
|
;;; ob-perl.el ends here
|