4

我正在尝试创建这个 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.
4

1 回答 1

4

这是解决方案:

#+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 块的文件名。
于 2014-10-22T08:20:26.620 回答