org-babel-perl.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ;;; org-babel-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 'org-babel)
  26. (org-babel-add-interpreter "perl")
  27. (add-to-list 'org-babel-tangle-langs '("perl" "pl" "#!/usr/bin/env perl"))
  28. (defun org-babel-execute:perl (body params)
  29. "Execute a block of Perl code with org-babel. This function is
  30. called by `org-babel-execute-src-block' via multiple-value-bind."
  31. (message "executing Perl source code block")
  32. (let ((full-body (concat
  33. (mapconcat ;; define any variables
  34. (lambda (pair)
  35. (format "$%s=%s;"
  36. (car pair)
  37. (org-babel-perl-var-to-perl (cdr pair))))
  38. vars "\n") "\n" (org-babel-trim body) "\n")) ;; then the source block body
  39. (session (org-babel-perl-initiate-session session)))
  40. (org-babel-perl-evaluate session full-body result-type)))
  41. (defun org-babel-prep-session:perl (session params)
  42. "Prepare SESSION according to the header arguments specified in PARAMS."
  43. (error "Sessions are not supported for Perl."))
  44. ;; helper functions
  45. (defun org-babel-perl-var-to-perl (var)
  46. "Convert an elisp var into a string of perl source code
  47. specifying a var of the same value."
  48. (if (listp var)
  49. (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
  50. (format "%S" var)))
  51. (defvar org-babel-perl-buffers '(:default . nil))
  52. (defun org-babel-perl-initiate-session (&optional session)
  53. "Simply return nil, as sessions are not supported by perl"
  54. nil)
  55. (defvar org-babel-perl-wrapper-method
  56. "
  57. sub main {
  58. %s
  59. }
  60. @r = main;
  61. open(o, \">%s\");
  62. print o join(\"\\n\", @r), \"\\n\"")
  63. (defvar org-babel-perl-pp-wrapper-method
  64. nil)
  65. (defun org-babel-perl-evaluate (session body &optional result-type)
  66. "Pass BODY to the Perl process in SESSION. If RESULT-TYPE equals
  67. 'output then return a list of the outputs of the statements in
  68. BODY, if RESULT-TYPE equals 'value then return the value of the
  69. last statement in BODY, as elisp."
  70. (if (not session)
  71. ;; external process evaluation
  72. (save-window-excursion
  73. (case result-type
  74. (output
  75. (with-temp-buffer
  76. (insert body)
  77. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  78. (shell-command-on-region (point-min) (point-max) "perl" 'replace)
  79. (buffer-string)))
  80. (value
  81. (let ((tmp-file (make-temp-file "perl-functional-results")))
  82. (with-temp-buffer
  83. (insert
  84. (format
  85. (if (member "pp" result-params)
  86. (error "Pretty-printing not implemented for perl")
  87. org-babel-perl-wrapper-method)
  88. (let ((lines (split-string
  89. (org-remove-indentation (org-babel-trim body)) "[\r\n]")))
  90. (concat
  91. (mapconcat #'identity (butlast lines) "\n")
  92. (format "\nreturn %s" (car (last lines)))))
  93. tmp-file))
  94. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  95. (shell-command-on-region (point-min) (point-max) "perl"))
  96. (let ((raw (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
  97. (if (or (member "code" result-params) (member "pp" result-params))
  98. raw
  99. (org-babel-import-elisp-from-file tmp-file)))))))
  100. ;; comint session evaluation
  101. (error "Sessions are not supported for Perl.")))
  102. (provide 'org-babel-perl)
  103. ;;; org-babel-perl.el ends here