e r l a n g : c o o k b o o k

/ Erlang.IncludingSourceCode

This Web


WebHome 
WebChanges 
TOC
NewRecipe 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Erlang Links


Erlang.org
Erlang Wiki
ErlMan
Erlang Wiki
The Jungerl
Erlang-fr.org
Joe Armstrong
Lambda the Ultimate

Erlang Web Ring


[Prev]: Joe Armstrong's Page
[Next]: Joe Armstrong's Page

Including Source Code in the Cookbook

Keywords and Inline Expressions

Keywords and other inline expressions can be highlighted and rendered in fixed font by surrounding them with "=" signs. For example, the notorious function name, receive {P, NP} -> ok end, was just embedded there using the following syntax:

=receive {P, NP} -> ok end=

Small Source Code Snippets

Small source code snippets can be formatted using the SyntaxHighlightingPlugin syntax, for example:

%begin erlang%
consumer(0, NC, _, Main) -> Main ! {self(), NC}; 
consumer(N, NC, P, Main) -> 
    P ! {self(), next},             %% signal buffer empty 
    receive                         %% receive buffer full 
        {data, Value} -> consumer(N-1, NC+1, P, Main) %% empty buffer 
    end. 
%end%

This results in the code being displayed as follows:

consumer(0, NC, _, Main) -> Main ! {self(), NC}; 
consumer(N, NC, P, Main) -> 
    P ! {self(), next},             %% signal buffer empty 
    receive                         %% receive buffer full 
        {data, Value} -> consumer(N-1, NC+1, P, Main) %% empty buffer 
    end. 

Including External Code

For any non-trivial code, and perhaps even for some trivial code, it's better to keep the code separately from the book text. Among other things, this will allow the code to be more easily run and tested within Erlang, as well as making it easier to package the code for distribution.

The current approach to including external code is that for each code sample, a topic should be created in the Erl web, for example Erl.Start1. Note that using a WikiName may be helpful, but it's not essential in this case. The raw source code should be stored in this topic - for now, simply cutting and pasting the source from a local copy should suffice, later we'll set up ways to manage this better.

Once the code is stored in a topic, it can be included in the book using syntax like this:

%begin erlang%
%INCLUDE{"Erl.Start1"}%
%end%

This results in the following:

-- AntonVanStraaten - 31 Mar 2004

Comments

-- BrentAFulgham - 19 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: AuthorRecipes
TopicOrder: 050

 
 
Copyright © 2004 by the contributing authors. All material on the Erlang Cookbook web site is the property of the contributing authors.
This material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest