Emacs org mode: how to write literate programs with noweb syntax
I am trying to create this Perl program:
#!/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);
I created this org file:
#+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
But he created this:
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;
}
Problems:
- There is a blank line at the top of the file.
- The Noweb block has not been expanded, but placed at the bottom of the file.
- I don't understand how to write the name of the output file once at the top? Currently, I have to rewrite it for each unit:
:tangle test.pl
.
+3
source to share
1 answer
Here's the solution:
#+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
- To put
#!/usr/bin/perl
on the top line use:shebang #!/usr/bin/perl
- Noweb block names must not contain spaces. Replace them with "-".
- Enter a filename for the noweb root block.
+3
source to share