babel: smarter `org-babel-ref-split-args' -- fixes bug parsing indexed function-style...
[rgr-org-mode.git] / contrib / babel / lisp / langs / org-babel-perl.el
blob1ecac538863b92148899ef2fb5c5d0ea31b46def
1 ;;; org-babel-perl.el --- org-babel functions for perl evaluation
3 ;; Copyright (C) 2009 Dan Davison, Eric Schulte
5 ;; Author: Dan Davison, Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Org-Babel support for evaluating perl source code.
31 ;;; Code:
32 (require 'org-babel)
34 (org-babel-add-interpreter "perl")
36 (add-to-list 'org-babel-tangle-langs '("perl" "pl" "#!/usr/bin/env perl"))
38 (defun org-babel-execute:perl (body params)
39 "Execute a block of Perl code with org-babel. This function is
40 called by `org-babel-execute-src-block'."
41 (message "executing Perl source code block")
42 (let* ((processed-params (org-babel-process-params params))
43 (session (first processed-params))
44 (vars (second processed-params))
45 (result-params (third processed-params))
46 (result-type (fourth processed-params))
47 (full-body (concat
48 (mapconcat ;; define any variables
49 (lambda (pair)
50 (format "$%s=%s;"
51 (car pair)
52 (org-babel-perl-var-to-perl (cdr pair))))
53 vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
54 (session (org-babel-perl-initiate-session session)))
55 (org-babel-perl-evaluate session full-body result-type)))
57 (defun org-babel-prep-session:perl (session params)
58 "Prepare SESSION according to the header arguments specified in PARAMS."
59 (error "Sessions are not supported for Perl."))
61 ;; helper functions
63 (defun org-babel-perl-var-to-perl (var)
64 "Convert an elisp var into a string of perl source code
65 specifying a var of the same value."
66 (if (listp var)
67 (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
68 (format "%S" var)))
70 (defvar org-babel-perl-buffers '(:default . nil))
72 (defun org-babel-perl-initiate-session (&optional session)
73 "Simply return nil, as sessions are not supported by perl"
74 nil)
76 (defvar org-babel-perl-wrapper-method
78 sub main {
81 @r = main;
82 open(o, \">%s\");
83 print o join(\"\\n\", @r), \"\\n\"")
85 (defvar org-babel-perl-pp-wrapper-method
86 nil)
88 (defun org-babel-perl-evaluate (session body &optional result-type)
89 "Pass BODY to the Perl process in SESSION. If RESULT-TYPE equals
90 'output then return a list of the outputs of the statements in
91 BODY, if RESULT-TYPE equals 'value then return the value of the
92 last statement in BODY, as elisp."
93 (if (not session)
94 ;; external process evaluation
95 (save-window-excursion
96 (case result-type
97 (output
98 (with-temp-buffer
99 (insert body)
100 ;; (message "buffer=%s" (buffer-string)) ;; debugging
101 (shell-command-on-region (point-min) (point-max) "perl" 'replace)
102 (buffer-string)))
103 (value
104 (let ((tmp-file (make-temp-file "perl-functional-results")))
105 (with-temp-buffer
106 (insert
107 (format
108 (if (member "pp" result-params)
109 (error "Pretty-printing not implemented for perl")
110 org-babel-perl-wrapper-method)
111 (mapconcat
112 (lambda (line) (format "\t%s" line))
113 (split-string
114 (org-remove-indentation (org-babel-trim body)) "[\r\n]") "\n")
115 tmp-file))
116 ;; (message "buffer=%s" (buffer-string)) ;; debugging
117 (shell-command-on-region (point-min) (point-max) "perl"))
118 (let ((raw (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
119 (if (or (member "code" result-params) (member "pp" result-params))
121 (org-babel-import-elisp-from-file tmp-file)))))))
122 ;; comint session evaluation
123 (error "Sessions are not supported for Perl.")))
125 (provide 'org-babel-perl)
126 ;;; org-babel-perl.el ends here