Skip to content

The init and main commands

tim-hardcastle edited this page Nov 14, 2024 · 5 revisions

Pipefish gives special privileges to two commands. If you write a command called init having no parameters, then when the script is run, Pipefish will call init. And if you write a function called main having no parameters, then Pipefish will call main after init (or will just call main if init doesn't exist).

Into init you should put all the code that sets things up; slurping information from a file into a global variable, or creating a SQL table; etc. Into main you should put all the code after setup that interacts with the end-user.

Ideally, init should be idempotent, i.e. running it twice has the same effect as running it once. If this is not the case, you should think about why not.

You may wonder why Pipefish can't just get by with main like other languages. The point is to make REPL-based development easier. When you're livecoding, and Pipefish rebuilds the service, it will call init (if it exists), but will not call main. So because only init is called, you can then use the REPL to test and prod and poke all the commands and functions of the script with everything fully initialized, but without you being dragged into interacting with your service like you were the end-user.

If you want to call the main command in this situation, you can of course just type main into the REPL and call it!

Clone this wiki locally