-
Notifications
You must be signed in to change notification settings - Fork 6
The init and main commands
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!
🧿 Pipefish is distributed under the MIT license. Please steal my code and ideas.
- Getting started
- Language basics
- The type system and built-in functions
- Functional Pipefish
- Encapsulation
- Imperative Pipefish
-
Imports and libraries
- The files library
- The fmt library
- The html library
- The math library
- The math/big library
- The math/cmplx library
- The math/rand library
- The path library
- The path/filepath library
- The reflect library
- The regexp library
- The sql library
- The strings library
- The terminal library
- The time library
- The unicode library
- Advanced Pipefish
- Developing in Pipefish
- Deployment
- Appendices