# WorkingWiki/Script Examples

This page includes three examples of using WorkingWiki to edit and run actual code, and display the results.

## Hello, World

Here's a little perl script:

example.pl
my $hello = "Hello, weird little world!";$hello =~ s/Hello/Hi/; # substitute the first word of the message
print("\$hello\n");

Here's the perl script's output:

example-output.txt
Hi, weird little world!

And here's the makefile that makes it run:

Makefile
example-output.txt : example.pl
perl example.pl > example-output.txt

[Explanation of the makefile: The first line says that the output file should be remade whenever the perl script is changed. '>' is a UNIX directive that captures the output from the command to its left and puts it in the file to its right. The makefile invokes the perl interpreter to run the program "example.pl" and capture its output in "example-output.txt", but only if the program has changed since the last time the output file was updated. WorkingWiki uses this makefile every time it displays the output file, to make sure the file is up to date. So if you edit the perl code, and then save or preview this wiki page, the output file will be up to date when it appears in the page.]

If you're logged in to the wiki, you can change the script and watch the output change, it's fun!

(Because of software security issues, we can't let anonymous users edit the project files, sorry. If you're not logged in, you may want to click the "view source" tab above, to see how the files are embedded in the wiki page.)

## Graphics example

Let's do this one with Python. We could do it with R, which is the main language in use on the theobio wikis, but we have special rules for using R, and we want to offer a self-contained example here.

So here's a Python script that makes a figure:

figure-script.py
import matplotlib
matplotlib.use('agg')
from pylab import *

figure(figsize=(5,4))
t = arange(-8, 8, 0.01)
plot( t, t*sin(t), '-' )

savefig('figure-example.png', format='png')

And here's the makefile to run the python script:

Makefile
figure-example.png : figure-script.py
python figure-script.py

And now we ask for the figure and WorkingWiki makes it for us by running the script:

If you look at the source text of this page, you'll notice this example is in a separate project from the other one. This is just so we avoid the extra complexity of writing about how to combine makefiles in a single project.

## R graphics example

Here is a simple example of how to do a figure with R, using the lalashan site's custom rules for using R. The custom rules make it simple: just define a .R file:

example.R
plot(function(x){-x*cos(x-1)}, -pi, pi, col="blue");

and request its output using just the right filename:

log, pdf