1 ;;; ob-perl.el --- Babel Functions for Perl -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 ;; Authors: Dan Davison
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: https://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <https://www.gnu.org/licenses/>.
27 ;; Org-Babel support for evaluating perl source code.
32 (defvar org-babel-tangle-lang-exts
)
33 (add-to-list 'org-babel-tangle-lang-exts
'("perl" .
"pl"))
35 (defvar org-babel-default-header-args
:perl
'())
37 (defvar org-babel-perl-command
"perl"
38 "Name of command to use for executing perl code.")
40 (defun org-babel-execute:perl
(body params
)
41 "Execute a block of Perl code with Babel.
42 This function is called by `org-babel-execute-src-block'."
43 (let* ((session (cdr (assq :session params
)))
44 (result-params (cdr (assq :result-params params
)))
45 (result-type (cdr (assq :result-type params
)))
46 (full-body (org-babel-expand-body:generic
47 body params
(org-babel-variable-assignments:perl params
)))
48 (session (org-babel-perl-initiate-session session
)))
49 (org-babel-reassemble-table
50 (org-babel-perl-evaluate session full-body result-type result-params
)
52 (cdr (assq :colname-names params
)) (cdr (assq :colnames params
)))
54 (cdr (assq :rowname-names params
)) (cdr (assq :rownames params
))))))
56 (defun org-babel-prep-session:perl
(_session _params
)
57 "Prepare SESSION according to the header arguments in PARAMS."
58 (error "Sessions are not supported for Perl"))
60 (defun org-babel-variable-assignments:perl
(params)
61 "Return list of perl statements assigning the block's variables."
64 (org-babel-perl--var-to-perl (cdr pair
) (car pair
)))
65 (org-babel--get-vars params
)))
69 (defvar org-babel-perl-var-wrap
"q(%s)"
70 "Wrapper for variables inserted into Perl code.")
72 (defvar org-babel-perl--lvl
)
73 (defun org-babel-perl--var-to-perl (var &optional varn
)
74 "Convert an elisp value to a perl variable.
75 The elisp value, VAR, is converted to a string of perl source code
76 specifying a var of the same value."
78 (let ((org-babel-perl--lvl 0) (lvar (listp var
)))
79 (concat "my $" (symbol-name varn
) "=" (when lvar
"\n")
80 (org-babel-perl--var-to-perl var
)
82 (let ((prefix (make-string (* 2 org-babel-perl--lvl
) ?\
)))
85 (let ((org-babel-perl--lvl (1+ org-babel-perl--lvl
)))
87 (mapconcat #'org-babel-perl--var-to-perl var
"")
90 (unless (zerop org-babel-perl--lvl
) ",\n")))))
92 (defvar org-babel-perl-buffers
'(:default . nil
))
94 (defun org-babel-perl-initiate-session (&optional _session _params
)
95 "Return nil because sessions are not supported by perl."
98 (defvar org-babel-perl-wrapper-method
"{
102 open my $BOH, qq(>%s) or die qq(Perl: Could not open output file.$/);
103 my $rv = &$babel_sub();
106 if (qq(ARRAY) eq $rt) {
109 foreach my $rv ( @$rv ) {
111 if (qq(ARRAY) eq $rt) {
122 (defvar org-babel-perl-preface nil
)
124 (defvar org-babel-perl-pp-wrapper-method
127 (defun org-babel-perl-evaluate (session ibody
&optional result-type result-params
)
128 "Pass BODY to the Perl process in SESSION.
129 If RESULT-TYPE equals `output' then return a list of the outputs
130 of the statements in BODY, if RESULT-TYPE equals `value' then
131 return the value of the last statement in BODY, as elisp."
132 (when session
(error "Sessions are not supported for Perl"))
133 (let* ((body (concat org-babel-perl-preface ibody
))
134 (tmp-file (org-babel-temp-file "perl-"))
135 (tmp-babel-file (org-babel-process-file-name
140 (with-temp-file tmp-file
142 (org-babel-eval org-babel-perl-command body
))
145 (org-babel-eval org-babel-perl-command
146 (format org-babel-perl-wrapper-method
147 body tmp-babel-file
))))))
149 (org-babel-result-cond result-params
150 (org-babel-eval-read-file tmp-file
)
151 (org-babel-import-elisp-from-file tmp-file
'(16)))))))
157 ;;; ob-perl.el ends here