ob-perl.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; ob-perl.el --- org-babel functions for perl evaluation
  2. ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
  3. ;; Authors: Dan Davison
  4. ;; Eric Schulte
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  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. (eval-when-compile (require 'cl))
  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 (assoc :session params)))
  32. (result-params (cdr (assoc :result-params params)))
  33. (result-type (cdr (assoc :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)
  39. (org-babel-pick-name
  40. (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
  41. (org-babel-pick-name
  42. (cdr (assoc :rowname-names params)) (cdr (assoc :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. (format "$%s=%s;"
  51. (car pair)
  52. (org-babel-perl-var-to-perl (cdr pair))))
  53. (mapcar #'cdr (org-babel-get-header params :var))))
  54. ;; helper functions
  55. (defvar org-babel-perl-var-wrap "q(%s)"
  56. "Wrapper for variables inserted into Perl code.")
  57. (defun org-babel-perl-var-to-perl (var)
  58. "Convert an elisp value to a perl variable.
  59. The elisp value, VAR, is converted to a string of perl source code
  60. specifying a var of the same value."
  61. (if (listp var)
  62. (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
  63. (format org-babel-perl-var-wrap var)))
  64. (defvar org-babel-perl-buffers '(:default . nil))
  65. (defun org-babel-perl-initiate-session (&optional session params)
  66. "Return nil because sessions are not supported by perl."
  67. nil)
  68. (defvar org-babel-perl-wrapper-method
  69. "
  70. sub main {
  71. %s
  72. }
  73. @r = main;
  74. open(o, \">%s\");
  75. print o join(\"\\n\", @r), \"\\n\"")
  76. (defvar org-babel-perl-pp-wrapper-method
  77. nil)
  78. (defun org-babel-perl-evaluate (session body &optional result-type)
  79. "Pass BODY to the Perl process in SESSION.
  80. If RESULT-TYPE equals 'output then return a list of the outputs
  81. of the statements in BODY, if RESULT-TYPE equals 'value then
  82. return the value of the last statement in BODY, as elisp."
  83. (when session (error "Sessions are not supported for Perl"))
  84. (case result-type
  85. (output (org-babel-eval org-babel-perl-command body))
  86. (value (let ((tmp-file (org-babel-temp-file "perl-")))
  87. (org-babel-eval
  88. org-babel-perl-command
  89. (format org-babel-perl-wrapper-method body
  90. (org-babel-process-file-name tmp-file 'noquote)))
  91. (org-babel-eval-read-file tmp-file)))))
  92. (provide 'ob-perl)
  93. ;;; ob-perl.el ends here