Re: [O] [PATCH] bug in expansion of variables in babel Perl
Achim Gratz writes: > Here are two patches that fix this and implement (partly) some of your > suggestions. I don't think Org should pollute the global Perl namespace > by default, so I've left the definition of org-babel-perl-preface to the > user for now. The second patch has the debugging aid you've been > requesting, if you bind the symbol org-babel--debug-input to anything > the temporary input files won't be deleted after the code has run. I've pushed these changes with some extended babel support for Perl: the results are now interpreted by babel, like for most other languages. Also, the wrapper has been extended to be a bit smarter: if the return value is an arrayref (or arrayref of arrayrefs), the wrapper automatically formats the output so that it will be interpreted as a table (a simple arrayref will be interpreted as a column vector). In particular that means you can pass a table through a Perl source block with exactly no code to write (as the variable that is input will be returned directly). In all other cases the return value is simply printed, so it should be a scalar. I've kept the wrapper as an (anonymous) subroutine, so if anybody had latched onto "return $rv;" it will continue to work. Also a few tests were added. Regards, Achim. -- +<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+ SD adaptations for Waldorf Q V3.00R3 and Q+ V3.54R2: http://Synth.Stromeko.net/Downloads.html#WaldorfSDada
Re: [O] [PATCH] bug in expansion of variables in babel Perl
D M German uvic.ca> writes: > print $BO join($/, @r), $/ ; Sorry, this should really be: print $BO join(qq($/), @r), qq($/); Anyway, I think I'll have to rework the wrapper to be an anonymous subroutine so that the (reasonably expactable) "return @foo;" at the end of the program (or in the middle if so desired) continues to work. Also, I just checked that noweb syntax does work for code execution, so the prefix you wanted to have is easy enough to add via Library of Babel: #+name: prefix #+begin_src perl # Santa's little helpers #+end_src #+begin_src perl :noweb yes <> &help(); #+end_src In light of this, I think we should not implement the "preface" change as its effects can be had without obscuring things too much. Regards, Achim.
Re: [O] [PATCH] bug in expansion of variables in babel Perl
Achim> D M German uvic.ca> writes: >> I think the issue is that, at least in my computer the variable $\ >> returns empty (the record separator). Achim> Thinko on my side, what I wanted was the input record separator "$/" Achim> (to avoid Achim> specifying a literal newline for those systems where this is actually Achim> multi-character). Hi Achim, Once I changed it: (defvar org-babel-perl-wrapper-method "{ my @r = eval( q( %s )); open my $BO, qq(>%s) or die qq( Perl: Could not open output file.$\\ ); print $BO join($/, @r), $/ ; }") the result now has \n in between fields (literally): #+name: t_output_table #+begin_src perl :results table print "Test\n"; (1, 2) #+end_src #+RESULTS: t_output_table | 1\n2\n | what is the expected field separator for Org-babel? Achim> Regards, Achim> Achim. -- Daniel M. German "Prose is intrinsically linear; a good book carries the reader forward The Economist ->on the crest of the words" http://turingmachine.org/ http://silvernegative.com/ dmg (at) uvic (dot) ca replace (at) with @ and (dot) with .
Re: [O] [PATCH] bug in expansion of variables in babel Perl
D M German uvic.ca> writes: > I think the issue is that, at least in my computer the variable $\ > returns empty (the record separator). Thinko on my side, what I wanted was the input record separator "$/" (to avoid specifying a literal newline for those systems where this is actually multi-character). Regards, Achim.
Re: [O] [PATCH] bug in expansion of variables in babel Perl
Achim Gratz twisted the bytes to say: Hi Achim, thanks for taking the time to do this. I applied the patch, one of the hunks didn't apply due to Eric's changes, but that is not an issue, since they do the same: -- diff a/lisp/ob-perl.el b/lisp/ob-perl.el(rejected hunks) @@ -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)) -- Everything works as intended, except for the return value of the perl code. Values in the list are concatenated, as one: #+begin_src perl :results table (1, 2, 3) #+end_src #+RESULTS: | 123 | #+begin_src perl :results table (1, 2, 3) #+end_src #+RESULTS: | 123 | I think the issue is that, at least in my computer the variable $\ returns empty (the record separator). #+begin_src perl :results output print "value of \$\\ [$\]\n"; #+end_src #+RESULTS: #+begin_example value of $\ [] #+end_example --daniel -- Daniel M. German "Work. Finish. Publish. " Michael Faraday http://turingmachine.org/ http://silvernegative.com/ dmg (at) uvic (dot) ca replace (at) with @ and (dot) with .
Re: [O] [PATCH] bug in expansion of variables in babel Perl
D M German writes: > I found a bug in the Babel perl code. When a table is used as input, the > values of the table are not escaped. Here are two patches that fix this and implement (partly) some of your suggestions. I don't think Org should pollute the global Perl namespace by default, so I've left the definition of org-babel-perl-preface to the user for now. The second patch has the debugging aid you've been requesting, if you bind the symbol org-babel--debug-input to anything the temporary input files won't be deleted after the code has run. >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 >From 6827b07c0e8a03eea11d86ea714c8f10fb05b43d Mon Sep 17 00:00:00 2001 From: Achim Gratz Date: Sun, 24 Feb 2013 17:15:36 +0100 Subject: [PATCH 2/2] ob-eval: make org-babel--shell-command-on-region internal and simplify MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lisp/ob-eval.el (org-babel-eval): Use simplified version of `org-babel--shell-command-on-regionĀ“, we are the only caller of this function. * lisp/ob-eval.el (org-babel--shell-command-on-region): Replace `org-babel-shell-command-on-regionĀ“ with a much more simplified internal version, remove superfluous DOCSTRING and interacti