From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: bug in expansion of variables in babel Perl Date: Sun, 24 Feb 2013 09:52:27 -0700 Message-ID: <87a9qtk62c.fsf@gmail.com> References: <87ppzq138q.fsf@mn.cs.uvic.ca> <87txp1zz1f.fsf@Rainer.invalid> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([208.118.235.92]:46622) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U9epB-0005v2-DB for emacs-orgmode@gnu.org; Sun, 24 Feb 2013 11:53:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U9ep8-0006O4-8n for emacs-orgmode@gnu.org; Sun, 24 Feb 2013 11:53:17 -0500 Received: from mail-pb0-f45.google.com ([209.85.160.45]:46372) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U9ep7-0006Ns-V9 for emacs-orgmode@gnu.org; Sun, 24 Feb 2013 11:53:14 -0500 Received: by mail-pb0-f45.google.com with SMTP id ro8so1247604pbb.18 for ; Sun, 24 Feb 2013 08:53:12 -0800 (PST) In-Reply-To: <87txp1zz1f.fsf@Rainer.invalid> (Achim Gratz's message of "Sun, 24 Feb 2013 13:17:32 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Achim Gratz Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Achim Gratz writes: > D M German writes: >> 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 '). > > I think that's the right thing to do. There shouldn't be anything in > the table that needs to be interpolated by Perl while the variable is > defined. > >> + (format "'%s'" var))) > > A slightly more perlish way would be to use > (format "q(%s)" var))) > I just added the variable `org-babel-perl-var-wrap', into ob-perl.el ;; emacs-lisp (defvar org-babel-perl-var-wrap "q(%s)" "Wrapper for variables inserted into Perl code.") This way we will get what sounds like improved wrapping by default, but users who really do want to insert interpolated values can customize this variable. > >> 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) > > The other languages have the same problem, maybe there should be a > general option to mirror the commands into a source block in the org > file or to make the buffer with the program to be eval'ed stick around. > > Eric, WDYT? Are you familiar with `org-babel-expand-src-block' bound to C-c C-v v? If I understand the desire correctly, it should be what you're after. Perhaps an option to raise the expanded source code buffer along with the error buffer when an error is raised would be a useful addition. > Also, I'm not really sure why we need all the complexity of > shell-command-on-region when it looks like we should be able to > call-process-region ourselves. Modifying Babel to run (non-session > and perhaps optionally) from files instead of buffers seems to be a > more wide-reaching operation. > This complexity is related to the need to occasionally run in remote directories or on remote machines. If there are ways to reduce this complexity without losing functionality I'm game. > >> As we are into it, I found this declaration to be very useful.=20 > [=E2=80=A6] > > I think this is better done by altering org-babel-perl-command to > include "-Mstrict". If you put the helper functions into a module in > @INC or tell Perl where to find them, then you can add "-Mmyhelper" as > well. Here's a wrapper to match: > > (defvar org-babel-perl-wrapper-method > "{ > my @r =3D eval( q(%s) ); > open my $BO, qq(>%s)); > print $BO join($\\, @r), $\\ ; > }") > > For your problem with :results output blocks, I think it would be > possible to wrap them (a bit differently) also, but the "helper module" > above would also solve this problem, so let's see what Eric says since I > don't know if another language has already set a precedent of wrapping > these commands too. > I'm not currently aware of any language-wide support for printing the expanded code block along with the results. I don't think there has been any desire for this previously. It shouldn't be hard to write an emacs lisp block to give the desired result... Here's an example of such a function. Please forgive my complete lack of Perl knowledge (at one time I knew it well). Note that I had to make a small change to the return value of `org-babel-expand-src-block'. --=-=-= Content-Type: text/x-org; charset=utf-8 Content-Disposition: inline; filename=it.org Content-Transfer-Encoding: quoted-printable #+name: table | 1 | 2 | 3 | | 4 | 5 | 6 | #+name: perl-block #+begin_src perl :var table=3Dtable print $table #+end_src #+RESULTS: perl-block : 1 #+begin_src emacs-lisp :var block=3D"perl-block" :results raw (save-excursion (message "block is %S" block) (org-babel-goto-named-src-block block) (format "#+begin_example\n%S\n=E2=86=93\n%s\n#+end_example\n" (org-babel-expand-src-block) (org-babel-execute-src-block))) #+end_src #+RESULTS: #+begin_example "$table=3D[[q(1), q(2), q(3)], [q(4), q(5), q(6)]]; print $table" =E2=86=93 1 #+end_example --=-=-= Content-Type: text/plain > >> >> Finally, if interested, i can write a couple of examples for Perl that >> could help people who want to use it. > > Also a few tests would be highly welcome. > +1 Cheers, > > > Regards, > Achim. -- Eric Schulte http://cs.unm.edu/~eschulte --=-=-=--