ob-perl.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. ;;; ob-perl.el --- Babel Functions for Perl -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. ;; Authors: Dan Davison
  4. ;; Eric Schulte
  5. ;; Maintainer: Corwin Brust
  6. ;; Keywords: literate programming, reproducible research
  7. ;; Homepage: 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 'ob)
  23. (defvar org-babel-tangle-lang-exts)
  24. (add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
  25. (defvar org-babel-default-header-args:perl '())
  26. (defvar org-babel-perl-command "perl"
  27. "Name of command to use for executing perl code.")
  28. (defun org-babel-execute:perl (body params)
  29. "Execute a block of Perl code with Babel.
  30. This function is called by `org-babel-execute-src-block'."
  31. (let* ((session (cdr (assq :session params)))
  32. (result-params (cdr (assq :result-params params)))
  33. (result-type (cdr (assq :result-type params)))
  34. (full-body (org-babel-expand-body:generic
  35. body params (org-babel-variable-assignments:perl params)))
  36. (session (org-babel-perl-initiate-session session)))
  37. (org-babel-reassemble-table
  38. (org-babel-perl-evaluate session full-body result-type result-params)
  39. (org-babel-pick-name
  40. (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
  41. (org-babel-pick-name
  42. (cdr (assq :rowname-names params)) (cdr (assq :rownames params))))))
  43. (defun org-babel-prep-session:perl (_session _params)
  44. "Prepare SESSION according to the header arguments in PARAMS."
  45. (error "Sessions are not supported for Perl"))
  46. (defun org-babel-variable-assignments:perl (params)
  47. "Return list of perl statements assigning the block's variables."
  48. (mapcar
  49. (lambda (pair)
  50. (org-babel-perl--var-to-perl (cdr pair) (car pair)))
  51. (org-babel--get-vars params)))
  52. ;; helper functions
  53. (defvar org-babel-perl-var-wrap "q(%s)"
  54. "Wrapper for variables inserted into Perl code.")
  55. (defvar org-babel-perl--lvl)
  56. (defun org-babel-perl--var-to-perl (var &optional varn)
  57. "Convert an elisp value to a perl variable.
  58. The elisp value, VAR, is converted to a string of perl source code
  59. specifying a var of the same value."
  60. (if varn
  61. (let ((org-babel-perl--lvl 0) (lvar (listp var)))
  62. (concat "my $" (symbol-name varn) "=" (when lvar "\n")
  63. (org-babel-perl--var-to-perl var)
  64. ";\n"))
  65. (let ((prefix (make-string (* 2 org-babel-perl--lvl) ?\ )))
  66. (concat prefix
  67. (if (listp var)
  68. (let ((org-babel-perl--lvl (1+ org-babel-perl--lvl)))
  69. (concat "[\n"
  70. (mapconcat #'org-babel-perl--var-to-perl var "")
  71. prefix "]"))
  72. (format "q(%s)" var))
  73. (unless (zerop org-babel-perl--lvl) ",\n")))))
  74. (defvar org-babel-perl-buffers '(:default . nil))
  75. (defun org-babel-perl-initiate-session (&optional _session _params)
  76. "Return nil because sessions are not supported by perl."
  77. nil)
  78. (defvar org-babel-perl-wrapper-method "{
  79. my $babel_sub = sub {
  80. %s
  81. };
  82. open my $BOH, qq(>%s) or die qq(Perl: Could not open output file.$/);
  83. my $rv = &$babel_sub();
  84. my $rt = ref $rv;
  85. select $BOH;
  86. if (qq(ARRAY) eq $rt) {
  87. local $\\=$/;
  88. local $,=qq(\t);
  89. foreach my $rv ( @$rv ) {
  90. my $rt = ref $rv;
  91. if (qq(ARRAY) eq $rt) {
  92. print @$rv;
  93. } else {
  94. print $rv;
  95. }
  96. }
  97. } else {
  98. print $rv;
  99. }
  100. }")
  101. (defvar org-babel-perl-preface nil)
  102. (defvar org-babel-perl-pp-wrapper-method
  103. nil)
  104. (defun org-babel-perl-evaluate (session ibody &optional result-type result-params)
  105. "Pass BODY to the Perl process in SESSION.
  106. If RESULT-TYPE equals `output' then return a list of the outputs
  107. of the statements in BODY, if RESULT-TYPE equals `value' then
  108. return the value of the last statement in BODY, as elisp."
  109. (when session (error "Sessions are not supported for Perl"))
  110. (let* ((body (concat org-babel-perl-preface ibody))
  111. (tmp-file (org-babel-temp-file "perl-"))
  112. (tmp-babel-file (org-babel-process-file-name
  113. tmp-file 'noquote)))
  114. (let ((results
  115. (pcase result-type
  116. (`output
  117. (with-temp-file tmp-file
  118. (insert
  119. (org-babel-eval org-babel-perl-command body))
  120. (buffer-string)))
  121. (`value
  122. (org-babel-eval org-babel-perl-command
  123. (format org-babel-perl-wrapper-method
  124. body tmp-babel-file))))))
  125. (when results
  126. (org-babel-result-cond result-params
  127. (org-babel-eval-read-file tmp-file)
  128. (org-babel-import-elisp-from-file tmp-file '(16)))))))
  129. (provide 'ob-perl)
  130. ;;; ob-perl.el ends here