ob-perl.el 5.2 KB

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