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
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)
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.
29 ;; Org-Babel support for evaluating perl source code.
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
))
48 (mapconcat ;; define any variables
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."))
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."
67 (concat "[" (mapconcat #'org-babel-perl-var-to-perl 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"
76 (defvar org-babel-perl-wrapper-method
83 print o join(\"\\n\", @r), \"\\n\"")
85 (defvar org-babel-perl-pp-wrapper-method
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."
94 ;; external process evaluation
95 (save-window-excursion
100 ;; (message "buffer=%s" (buffer-string)) ;; debugging
101 (shell-command-on-region (point-min) (point-max) "perl" 'replace
)
104 (let ((tmp-file (make-temp-file "perl-functional-results")))
108 (if (member "pp" result-params
)
109 (error "Pretty-printing not implemented for perl")
110 org-babel-perl-wrapper-method
)
112 (lambda (line) (format "\t%s" line
))
114 (org-remove-indentation (org-babel-trim body
)) "[\r\n]") "\n")
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