emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* bug in expansion of variables in babel Perl
@ 2013-02-24  9:16 D M German
  2013-02-24  9:45 ` dmg
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: D M German @ 2013-02-24  9:16 UTC (permalink / raw)
  To: emacs-orgmode


Hi Everybody,

I found a bug in the Babel perl code. When a table is used as input, the
values of the table  are not escaped. In fact, they are surrounded by
double quotes " instead of single ones '. This means that special
characters are interpreted: $<string>, and @<variable are considered
variables. See below.


For example:
----------------------------------------------------------------------
#+RESULTS: patito
| alias                                                | uniname                |
|------------------------------------------------------+------------------------|
| Jon  <tixy@xyz.org>                                  | jon


#+name: output(data=patito)
#+begin_src perl :results output
print "Begin\n";
print $$data[0][0], "\n";
print "End\n";
#+end_src                                                                                                                                                                

#+RESULTS: output
: Begin
: Jon  <tixy.org>
: End   

----------------------------------------------------------------------

I see two ways to solve this. The first is simply to replace the output
format of the variable from "%S" to "'%s'" (use quotes '). The other one
is to optionally escape the fields of the table (which is more
complicated, and would require replacing each). The third one is a
combination of both: replace them only if desired, via some header
configuration variable.

diff --git a/lisp/ob-perl.el b/lisp/ob-perl.el
index ccd3826..2f795aa 100644
--- a/lisp/ob-perl.el
+++ b/lisp/ob-perl.el
@@ -75,7 +75,7 @@ The elisp value, VAR, is converted to a string of perl source code
 specifying a var of the same value."
   (if (listp var)
       (concat "[" (mapconcat #'org-babel-perl-var-to-perl var ", ") "]")
-    (format "%S" var)))
+    (format "'%s'" var)))



Debugging perl is very cumbersome in org-mode. It would be nice to have
a feature to export the source to a file. This is because the variable
expansion needs to be done before the code can be used (hence simply cut
and paste does not work, nor shell-command-on-region)

I used the org-babel-perl-command variable to replace perl with a script
that simply wrote to a file. 

It would be nice to be able to write the script created by org a file,
so this can be debugged (it would have the variable definitions). Maybe
this is already a feature and I don't know about it.

As we are into it, I found this declaration to be very useful. 

----------------------------------------------------------------------
(setq org-babel-perl-wrapper-method
  "
use strict;

sub org_columns
{
    my ($table) = @_;
    my $y = $$table[0];
    return scalar(@$y);
}

sub org_rows
{
    my ($table) = @_;
    return scalar(@$table);
}

sub main {
%s
}

my @r = main;
open(o, \">%s\");
print o join(\"\\n\", @r), \"\\n\"")
----------------------------------------------------------------------

It does two things: it uses strict, so undeclared variables create
errors, and it also creates two functions: org_columns and org_rows
that, when used on the variable declared as input, return its number of
columns and rows:

my $rows = org_rows($data);
my $columns = org_columns($data);

the only problem with using strict is that variables would have to be
defined with "my" too: so that would require this patch:

diff --git a/lisp/ob-perl.el b/lisp/ob-perl.el
index ccd3826..82f8086 100644
--- a/lisp/ob-perl.el
+++ b/lisp/ob-perl.el
@@ -62,7 +62,7 @@ This function is called by `org-babel-execute-src-block'."
   "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))))


Finally, if interested, i can write a couple of examples for Perl that
could help people who want to use it. 

thanks again,

--
Daniel M. German                  "Great algorithms are
   Francis Sullivan ->             the poetry of computation"
http://turingmachine.org/
http://silvernegative.com/
dmg (at) uvic (dot) ca
replace (at) with @ and (dot) with .

 

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2013-03-02 22:02 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-24  9:16 bug in expansion of variables in babel Perl D M German
2013-02-24  9:45 ` dmg
2013-02-24 10:23   ` D M German
2013-02-24 13:08     ` Achim Gratz
2013-02-24 18:20       ` D M German
2013-02-24 12:17 ` Achim Gratz
2013-02-24 16:52   ` Eric Schulte
2013-02-24 17:15     ` Achim Gratz
2013-02-24 18:03     ` Achim Gratz
2013-02-25  9:44       ` D M German
2013-02-24 17:05 ` [PATCH] " Achim Gratz
2013-02-25  9:42   ` D M German
2013-02-25 12:48     ` Achim Gratz
2013-02-25 21:54       ` D M German
2013-02-26 11:13         ` Achim Gratz
2013-03-02 22:01   ` Achim Gratz

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).