ob-perl.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ;;; ob-perl.el --- Babel Functions for Perl -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2017 Free Software Foundation, Inc.
  3. ;; Authors: Dan Davison
  4. ;; Eric Schulte
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating perl source code.
  20. ;;; Code:
  21. (require 'ob)
  22. (defvar org-babel-tangle-lang-exts)
  23. (add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
  24. (defvar org-babel-default-header-args:perl '())
  25. (defvar org-babel-perl-command "perl"
  26. "Name of command to use for executing perl code.")
  27. (defun org-babel-execute:perl (body params)
  28. "Execute a block of Perl code with Babel.
  29. This function is called by `org-babel-execute-src-block'."
  30. (let* ((session (cdr (assq :session params)))
  31. (result-params (cdr (assq :result-params params)))
  32. (result-type (cdr (assq :result-type params)))
  33. (full-body (org-babel-expand-body:generic
  34. body params (org-babel-variable-assignments:perl params)))
  35. (session (org-babel-perl-initiate-session session)))
  36. (org-babel-reassemble-table
  37. (org-babel-perl-evaluate session full-body result-type result-params)
  38. (org-babel-pick-name
  39. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  40. (org-babel-pick-name
  41. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
  42. (defun org-babel-prep-session:perl (_session _params)
  43. "Prepare SESSION according to the header arguments in PARAMS."
  44. (error "Sessions are not supported for Perl"))
  45. (defun org-babel-variable-assignments:perl (params)
  46. "Return list of perl statements assigning the block's variables."
  47. (mapcar
  48. (lambda (pair)
  49. (org-babel-perl--var-to-perl (cdr pair) (car pair)))
  50. (org-babel--get-vars params)))
  51. ;; helper functions
  52. (defvar org-babel-perl-var-wrap "q(%s)"
  53. "Wrapper for variables inserted into Perl code.")
  54. (defvar org-babel-perl--lvl)
  55. (defun org-babel-perl--var-to-perl (var &optional varn)
  56. "Convert an elisp value to a perl variable.
  57. The elisp value, VAR, is converted to a string of perl source code
  58. specifying a var of the same value."
  59. (if varn
  60. (let ((org-babel-perl--lvl 0) (lvar (listp var)))
  61. (concat "my $" (symbol-name varn) "=" (when lvar "\n")
  62. (org-babel-perl--var-to-perl var)
  63. ";\n"))
  64. (let ((prefix (make-string (* 2 org-babel-perl--lvl) ?\ )))
  65. (concat prefix
  66. (if (listp var)
  67. (let ((org-babel-perl--lvl (1+ org-babel-perl--lvl)))
  68. (concat "[\n"
  69. (mapconcat #'org-babel-perl--var-to-perl var "")
  70. prefix "]"))
  71. (format "q(%s)" var))
  72. (unless (zerop org-babel-perl--lvl) ",\n")))))
  73. (defvar org-babel-perl-buffers '(:default . nil))
  74. (defun org-babel-perl-initiate-session (&optional _session _params)
  75. "Return nil because sessions are not supported by perl."
  76. nil)
  77. (defvar org-babel-perl-wrapper-method "{
  78. my $babel_sub = sub {
  79. %s
  80. };
  81. open my $BOH, qq(>%s) or die qq(Perl: Could not open output file.$/);
  82. my $rv = &$babel_sub();
  83. my $rt = ref $rv;
  84. select $BOH;
  85. if (qq(ARRAY) eq $rt) {
  86. local $\\=$/;
  87. local $,=qq(\t);
  88. foreach my $rv ( @$rv ) {
  89. my $rt = ref $rv;
  90. if (qq(ARRAY) eq $rt) {
  91. print @$rv;
  92. } else {
  93. print $rv;
  94. }
  95. }
  96. } else {
  97. print $rv;
  98. }
  99. }")
  100. (defvar org-babel-perl-preface nil)
  101. (defvar org-babel-perl-pp-wrapper-method
  102. nil)
  103. (defun org-babel-perl-evaluate (session ibody &optional result-type result-params)
  104. "Pass BODY to the Perl process in SESSION.
  105. If RESULT-TYPE equals `output' then return a list of the outputs
  106. of the statements in BODY, if RESULT-TYPE equals `value' then
  107. return the value of the last statement in BODY, as elisp."
  108. (when session (error "Sessions are not supported for Perl"))
  109. (let* ((body (concat org-babel-perl-preface ibody))
  110. (tmp-file (org-babel-temp-file "perl-"))
  111. (tmp-babel-file (org-babel-process-file-name
  112. tmp-file 'noquote)))
  113. (let ((results
  114. (pcase result-type
  115. (`output
  116. (with-temp-file tmp-file
  117. (insert
  118. (org-babel-eval org-babel-perl-command body))
  119. (buffer-string)))
  120. (`value
  121. (org-babel-eval org-babel-perl-command
  122. (format org-babel-perl-wrapper-method
  123. body tmp-babel-file))))))
  124. (when results
  125. (org-babel-result-cond result-params
  126. (org-babel-eval-read-file tmp-file)
  127. (org-babel-import-elisp-from-file tmp-file '(16)))))))
  128. (provide 'ob-perl)
  129. ;;; ob-perl.el ends here