From 7a668942b58dc94994b11e6ec0751ec36b07b196 Mon Sep 17 00:00:00 2001 From: Achim Gratz Date: Sun, 24 Feb 2013 13:28:50 +0100 Subject: [PATCH 1/2] ob-perl: modify variable definition to be compatible with strict and use non-interpolating quotes * lisp/ob-perl.el (org-babel-variable-assignments:perl): Add "my" to variable declaration so that it becomes compatible with "use strict;". * lisp/ob-perl.el (org-babel-perl-var-to-perl): Use Perl non-interpolating quoting on the string that defines the variable to suppress spurious interpretation of it as Perl syntax. * lisp/ob-perl.el (org-babel-perl-wrapper-method): Use a block and declare all variables as "my", also use Perl quoting and the output record separator instead of a literal LF character. Do away with the subroutine definition and use eval instead. * lisp/ob-perl.el (org-babel-perl-preface): Content of this variable is prepended to body before invocation of perl. * lisp/ob-perl.el (org-babel-perl-evaluate): Rename input parameter body to ibody and let-bind body to concatentation of org-babel-perl-preface and ibody. Following a suggestion by Daniel M. German in http://thread.gmane.org/gmane.emacs.orgmode/66855. --- lisp/ob-perl.el | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lisp/ob-perl.el b/lisp/ob-perl.el index ccd3826..53f166e 100644 --- a/lisp/ob-perl.el +++ b/lisp/ob-perl.el @@ -62,7 +62,7 @@ (defun org-babel-variable-assignments:perl (params) "Return list of perl statements assigning the block's variables." (mapcar (lambda (pair) - (format "$%s=%s;" + (format "my $%s=%s;" (car pair) (org-babel-perl-var-to-perl (cdr pair)))) (mapcar #'cdr (org-babel-get-header params :var)))) @@ -75,7 +75,7 @@ (defun org-babel-perl-var-to-perl (var) specifying a var of the same value." (if (listp var) (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]") - (format "%S" var))) + (format "q(%s)" var))) (defvar org-babel-perl-buffers '(:default . nil)) @@ -84,31 +84,34 @@ (defun org-babel-perl-initiate-session (&optional session params) nil) (defvar org-babel-perl-wrapper-method - " -sub main { + "{ + my @r = eval( q( %s -} -@r = main; -open(o, \">%s\"); -print o join(\"\\n\", @r), \"\\n\"") + )); + open my $BO, qq(>%s) or die qq( Perl: Could not open output file.$\\ ); + print $BO join($\\, @r), $\\ ; +}") + +(defvar org-babel-perl-preface nil) (defvar org-babel-perl-pp-wrapper-method nil) -(defun org-babel-perl-evaluate (session body &optional result-type) +(defun org-babel-perl-evaluate (session ibody &optional result-type) "Pass BODY to the Perl process in SESSION. If RESULT-TYPE equals 'output then return a list of the outputs of the statements in BODY, if RESULT-TYPE equals 'value then return the value of the last statement in BODY, as elisp." (when session (error "Sessions are not supported for Perl")) - (case result-type - (output (org-babel-eval org-babel-perl-command body)) - (value (let ((tmp-file (org-babel-temp-file "perl-"))) - (org-babel-eval - org-babel-perl-command - (format org-babel-perl-wrapper-method body - (org-babel-process-file-name tmp-file 'noquote))) - (org-babel-eval-read-file tmp-file))))) + (let ((body (concat org-babel-perl-preface ibody))) + (case result-type + (output (org-babel-eval org-babel-perl-command body)) + (value (let ((tmp-file (org-babel-temp-file "perl-"))) + (org-babel-eval + org-babel-perl-command + (format org-babel-perl-wrapper-method body + (org-babel-process-file-name tmp-file 'noquote))) + (org-babel-eval-read-file tmp-file)))))) (provide 'ob-perl) -- 1.8.1.4