ob-perl.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ;;; ob-perl.el --- org-babel functions for perl evaluation
  2. ;; Copyright (C) 2009 Dan Davison, Eric Schulte
  3. ;; Author: Dan Davison, Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program 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. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating perl source code.
  24. ;;; Code:
  25. (require 'ob)
  26. (defun org-babel-expand-body:perl (body params &optional processed-params)
  27. "Expand BODY according to PARAMS, return the expanded body."
  28. (let ((vars (second (or processed-params (org-babel-process-params params)))))
  29. (concat
  30. (mapconcat ;; define any variables
  31. (lambda (pair)
  32. (format "$%s=%s;"
  33. (car pair)
  34. (org-babel-perl-var-to-perl (cdr pair))))
  35. vars "\n") "\n" (org-babel-trim body) "\n")))
  36. (defun org-babel-execute:perl (body params)
  37. "Execute a block of Perl code with org-babel. This function is
  38. called by `org-babel-execute-src-block'."
  39. (message "executing Perl source code block")
  40. (let* ((processed-params (org-babel-process-params params))
  41. (session (first processed-params))
  42. (vars (second processed-params))
  43. (result-params (third processed-params))
  44. (result-type (fourth processed-params))
  45. (full-body (org-babel-expand-body:perl
  46. body params processed-params)) ;; then the source block body
  47. (session (org-babel-perl-initiate-session session)))
  48. (org-babel-reassemble-table
  49. (org-babel-perl-evaluate session full-body result-type)
  50. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  51. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  52. (defun org-babel-prep-session:perl (session params)
  53. "Prepare SESSION according to the header arguments specified in PARAMS."
  54. (error "Sessions are not supported for Perl."))
  55. ;; helper functions
  56. (defun org-babel-perl-var-to-perl (var)
  57. "Convert an elisp var into a string of perl source code
  58. specifying a var of the same value."
  59. (if (listp var)
  60. (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
  61. (format "%S" var)))
  62. (defvar org-babel-perl-buffers '(:default . nil))
  63. (defun org-babel-perl-initiate-session (&optional session params)
  64. "Simply return nil, as sessions are not supported by perl"
  65. nil)
  66. (defvar org-babel-perl-wrapper-method
  67. "
  68. sub main {
  69. %s
  70. }
  71. @r = main;
  72. open(o, \">%s\");
  73. print o join(\"\\n\", @r), \"\\n\"")
  74. (defvar org-babel-perl-pp-wrapper-method
  75. nil)
  76. (defun org-babel-perl-evaluate (session body &optional result-type)
  77. "Pass BODY to the Perl process in SESSION. If RESULT-TYPE equals
  78. 'output then return a list of the outputs of the statements in
  79. BODY, if RESULT-TYPE equals 'value then return the value of the
  80. last statement in BODY, as elisp."
  81. (if (not session)
  82. ;; external process evaluation
  83. (save-excursion
  84. (case result-type
  85. (output
  86. (with-temp-buffer
  87. (insert body)
  88. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  89. (org-babel-shell-command-on-region (point-min) (point-max) "perl" 'current-buffer 'replace)
  90. (buffer-string)))
  91. (value
  92. (let* ((tmp-file (make-temp-file "perl-functional-results")) exit-code
  93. (stderr
  94. (with-temp-buffer
  95. (insert
  96. (format
  97. (if (member "pp" result-params)
  98. (error "Pretty-printing not implemented for perl")
  99. org-babel-perl-wrapper-method) body tmp-file))
  100. (setq exit-code
  101. (org-babel-shell-command-on-region
  102. (point-min) (point-max) "perl" nil 'replace (current-buffer)))
  103. (buffer-string))))
  104. (if (> exit-code 0) (org-babel-error-notify exit-code stderr))
  105. (org-babel-import-elisp-from-file (org-babel-maybe-remote-file tmp-file))))))
  106. ;; comint session evaluation
  107. (error "Sessions are not supported for Perl.")))
  108. (provide 'ob-perl)
  109. ;;; ob-perl.el ends here