【问题标题】:Emacs org mode: how to create literate programs with noweb syntaxEmacs org 模式:如何使用 noweb 语法创建识字程序
【发布时间】:2014-10-21 18:01:18
【问题描述】:

我正在尝试创建这个 Perl 程序:

#!/usr/bin/perl

use strict;
use warnings;

open(my $fh, "<", "test.txt")
    or die "cannot open < file name: $!";

while (my $line = <$fh>) {
    print $line;
}

close($fh);

我创建了这个组织文件:

#+BABEL: :cache yes :tangle yes :noweb yes

#+NAME: top_block
#+begin_src perl :tangle test.pl
  #!/usr/bin/perl

  use strict;
  use warnings;

  open(my $fh, "<", "test.txt")
      or die "cannot open < file name: $!";
  <<output all the lines from file>>
  close($fh);
#+end_src

#+NAME: output all the lines from file
#+begin_src perl :tangle test.pl
  while (my $line = <$fh>) {
      print $line;
  }
#+end_src

但它创造了这个:

empty line here
#!/usr/bin/perl

use strict;
use warnings;

open(my $fh, "<", "test.txt")
    or die "cannot open < file name: $!";
<<output all the lines from file>>
close($fh);

while (my $line = <$fh>) {
    print $line;
}

问题:

  1. 文件顶部有一个空行。
  2. Noweb 块没有展开,而是放在文件底部。
  3. 没看懂,怎么把输出文件名写在最上面一次?目前,我必须为每个块重写它::tangle test.pl

【问题讨论】:

    标签: emacs org-mode literate-programming noweb


    【解决方案1】:

    解决办法如下:

    #+BABEL: :cache yes :tangle yes :noweb yes
    
    #+NAME: top_block
    #+begin_src perl :tangle "test.pl" :noweb tangle :shebang #!/usr/bin/perl
      use strict;
      use warnings;
    
      open(my $fh, "<", "test.txt")
          or die "cannot open < file name: $!";
      <<output-all>>
      close($fh);
    #+end_src
    
    #+NAME: output-all
    #+begin_src perl
      while (my $line = <$fh>) {
          print $line;
      }
    #+end_src
    
    1. 要将#!/usr/bin/perl 放在第一行,请使用:shebang #!/usr/bin/perl
    2. Noweb 块名称不应包含空格。将它们替换为“-”。
    3. 写入根 noweb 块的文件名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多