ob-perl.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; ob-perl.el --- org-babel functions for perl evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation
  3. ;; Author: Dan Davison, Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  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. (require 'ob-eval)
  23. (eval-when-compile (require 'cl))
  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-expand-body:perl (body params &optional processed-params)
  29. "Expand BODY according to PARAMS, return the expanded body."
  30. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  31. (concat
  32. (mapconcat ;; define any variables
  33. (lambda (pair)
  34. (format "$%s=%s;"
  35. (car pair)
  36. (org-babel-perl-var-to-perl (cdr pair))))
  37. vars "\n") "\n" (org-babel-trim body) "\n")))
  38. (defun org-babel-execute:perl (body params)
  39. "Execute a block of Perl code with Babel.
  40. This function is called by `org-babel-execute-src-block'."
  41. (let* ((processed-params (org-babel-process-params params))
  42. (session (nth 0 processed-params))
  43. (vars (nth 1 processed-params))
  44. (result-params (nth 2 processed-params))
  45. (result-type (nth 3 processed-params))
  46. (full-body (org-babel-expand-body:perl
  47. body params processed-params))
  48. (session (org-babel-perl-initiate-session session)))
  49. (org-babel-reassemble-table
  50. (org-babel-perl-evaluate session full-body result-type)
  51. (org-babel-pick-name
  52. (nth 4 processed-params) (cdr (assoc :colnames params)))
  53. (org-babel-pick-name
  54. (nth 5 processed-params) (cdr (assoc :rownames params))))))
  55. (defun org-babel-prep-session:perl (session params)
  56. "Prepare SESSION according to the header arguments in PARAMS."
  57. (error "Sessions are not supported for Perl."))
  58. ;; helper functions
  59. (defun org-babel-perl-var-to-perl (var)
  60. "Convert an elisp value to a perl variable.
  61. The elisp value, VAR, is converted to a string of perl source code
  62. specifying a var of the same value."
  63. (if (listp var)
  64. (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
  65. (format "%S" var)))
  66. (defvar org-babel-perl-buffers '(:default . nil))
  67. (defun org-babel-perl-initiate-session (&optional session params)
  68. "Return nil because sessions are not supported by perl"
  69. nil)
  70. (defvar org-babel-perl-wrapper-method
  71. "
  72. sub main {
  73. %s
  74. }
  75. @r = main;
  76. open(o, \">%s\");
  77. print o join(\"\\n\", @r), \"\\n\"")
  78. (defvar org-babel-perl-pp-wrapper-method
  79. nil)
  80. (defun org-babel-perl-evaluate (session body &optional result-type)
  81. "Pass BODY to the Perl process in SESSION.
  82. If RESULT-TYPE equals 'output then return a list of the outputs
  83. of the statements in BODY, if RESULT-TYPE equals 'value then
  84. return the value of the last statement in BODY, as elisp."
  85. (when session (error "Sessions are not supported for Perl."))
  86. (case result-type
  87. (output (org-babel-eval org-babel-perl-command body))
  88. (value (let ((tmp-file (make-temp-file "org-babel-perl-results-")))
  89. (org-babel-eval
  90. org-babel-perl-command
  91. (format org-babel-perl-wrapper-method body tmp-file))
  92. (org-babel-eval-read-file tmp-file)))))
  93. (provide 'ob-perl)
  94. ;; arch-tag: 88ef9396-d857-4dc3-8946-5a72bdfa2337
  95. ;;; ob-perl.el ends here