5

In an org file, I have a Perl source code block that generates SQL code. The Perl code is to be part of the exported document, but the SQL code is not; it is supposed to be tangled to the corresponding output source code file only. If my source block is as follows:

#+BEGIN_SRC perl     :results output verbatim code replace   :exports code   
:tangle no  :eval yes
  #!/path/to/perl

  # Do some computations, generates some SQL code.
  print "INSERT INTO mytable (id, num1, num2) VALUES";
  my $comma = '';
  for (my $i=1; $i<=4; $i++)
  {
      my $n1 = 20 + rand(20);
      my $n2 = 60 + rand(60);
      printf "$comma\n    (%5.2f, %6.2f)", $n1, $n2;
      $comma = ',';
  }
  print "\n";
#+END_SRC

Then when I evaluate the block with C-c C-c, the following results block is inserted:

#+RESULTS:
#+BEGIN_SRC perl
INSERT INTO mytable (id, num1, num2) VALUES
    (1, 21.03,  77.17),
    (2, 28.68,  83.04),
    (3, 31.16,  99.71),
    (4, 21.68,  88.94)
#+END_SRC

The problem is the +#BEGIN_SRC perl line. I want it to be

#+BEGIN_SRC sql    :eval no    :tangle yes    :exports none

The only way I've been able to do this is by changing the code header in the Perl source block to raw, and adding explicit Perl statements to generate the org headers for the output block:

#+BEGIN_SRC perl     :results output verbatim raw replace   :exports none   :tangle no  :eval yes
  print "#+BEGIN_SRC sql  :eval no   :tangle yes  :exports none\n";
  # ...REST OF CODE AS ABOVE, THEN:
  print "#+END_SRC\n";
#+END_SRC

This does generate the results code block I want, but it seems clumsy and ugly. Also, the hacky print statements are exported to the output document as part of the Perl code block. Is there a more elegant way to specify header options on a generated source code block?

1 Answer 1

8

:wrap will do what you want (cf. https://orgmode.org/manual/wrap.html).

Try this header.

#+BEGIN_SRC perl :results output verbatim code replace :exports code :tangle no :eval yes :wrap "SRC sql :eval no :tangle yes :exports none"

1
  • That did it! Thanks, I hadn't noticed that option before. Apr 3, 2018 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.