emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: D M German <dmg@uvic.ca>
To: emacs-orgmode@gnu.org
Subject: bug in expansion of variables in babel Perl
Date: Sun, 24 Feb 2013 01:16:05 -0800	[thread overview]
Message-ID: <87ppzq138q.fsf@mn.cs.uvic.ca> (raw)


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 .

 

             reply	other threads:[~2013-02-24  9:16 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-02-24  9:16 D M German [this message]
2013-02-24  9:45 ` bug in expansion of variables in babel Perl 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ppzq138q.fsf@mn.cs.uvic.ca \
    --to=dmg@uvic.ca \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).