ob-perl.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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: 0.01
  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. (add-to-list 'org-babel-tangle-lang-exts '("perl" . "pl"))
  23. (defvar org-babel-default-header-args:perl '())
  24. (defun org-babel-expand-body:perl (body params &optional processed-params)
  25. "Expand BODY according to PARAMS, return the expanded body."
  26. (let ((vars (nth 1 (or processed-params (org-babel-process-params params)))))
  27. (concat
  28. (mapconcat ;; define any variables
  29. (lambda (pair)
  30. (format "$%s=%s;"
  31. (car pair)
  32. (org-babel-perl-var-to-perl (cdr pair))))
  33. vars "\n") "\n" (org-babel-trim body) "\n")))
  34. (defun org-babel-execute:perl (body params)
  35. "Execute a block of Perl code with org-babel. This function is
  36. called by `org-babel-execute-src-block'."
  37. (message "executing Perl source code block")
  38. (let* ((processed-params (org-babel-process-params params))
  39. (session (first processed-params))
  40. (vars (nth 1 processed-params))
  41. (result-params (nth 2 processed-params))
  42. (result-type (nth 3 processed-params))
  43. (full-body (org-babel-expand-body:perl
  44. body params processed-params)) ;; then the source block body
  45. (session (org-babel-perl-initiate-session session)))
  46. (org-babel-reassemble-table
  47. (org-babel-perl-evaluate session full-body result-type)
  48. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  49. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  50. (defun org-babel-prep-session:perl (session params)
  51. "Prepare SESSION according to the header arguments specified in PARAMS."
  52. (error "Sessions are not supported for Perl."))
  53. ;; helper functions
  54. (defun org-babel-perl-var-to-perl (var)
  55. "Convert an elisp var into a string of perl source code
  56. specifying a var of the same value."
  57. (if (listp var)
  58. (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
  59. (format "%S" var)))
  60. (defvar org-babel-perl-buffers '(:default . nil))
  61. (defun org-babel-perl-initiate-session (&optional session params)
  62. "Simply return nil, as sessions are not supported by perl"
  63. nil)
  64. (defvar org-babel-perl-wrapper-method
  65. "
  66. sub main {
  67. %s
  68. }
  69. @r = main;
  70. open(o, \">%s\");
  71. print o join(\"\\n\", @r), \"\\n\"")
  72. (defvar org-babel-perl-pp-wrapper-method
  73. nil)
  74. (defun org-babel-perl-evaluate (session body &optional result-type)
  75. "Pass BODY to the Perl process in SESSION. If RESULT-TYPE equals
  76. 'output then return a list of the outputs of the statements in
  77. BODY, if RESULT-TYPE equals 'value then return the value of the
  78. last statement in BODY, as elisp."
  79. (if (not session)
  80. ;; external process evaluation
  81. (save-excursion
  82. (case result-type
  83. (output
  84. (with-temp-buffer
  85. (insert body)
  86. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  87. (org-babel-shell-command-on-region (point-min) (point-max) "perl" 'current-buffer 'replace)
  88. (buffer-string)))
  89. (value
  90. (let* ((tmp-file (make-temp-file "perl-functional-results")) exit-code
  91. (stderr
  92. (with-temp-buffer
  93. (insert
  94. (format
  95. (if (member "pp" result-params)
  96. (error "Pretty-printing not implemented for perl")
  97. org-babel-perl-wrapper-method) body tmp-file))
  98. (setq exit-code
  99. (org-babel-shell-command-on-region
  100. (point-min) (point-max) "perl" nil 'replace (current-buffer)))
  101. (buffer-string))))
  102. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  103. (org-babel-import-elisp-from-file (org-babel-maybe-remote-file tmp-file))))))
  104. ;; comint session evaluation
  105. (error "Sessions are not supported for Perl.")))
  106. (provide 'ob-perl)
  107. ;; arch-tag: 88ef9396-d857-4dc3-8946-5a72bdfa2337
  108. ;;; ob-perl.el ends here