|
| 1 | +.. _getting_started: |
| 2 | + |
1 | 3 | Getting started
|
2 | 4 | ===============
|
3 | 5 |
|
4 |
| -TODO: Getting started guide |
| 6 | +In this section, we will see how to get up and running with the Links REPL, as |
| 7 | +well as running the web examples. |
| 8 | + |
| 9 | +Running the REPL |
| 10 | +---------------- |
| 11 | + |
| 12 | +The Links REPL (Read-Eval-Print-Loop) allows you to run commands and evaluate |
| 13 | +expressions interactively. Typing ``linx`` on a terminal should bring up an |
| 14 | +interactive window:: |
| 15 | + |
| 16 | + _ _ __ _ _ __ ___ |
| 17 | + / | | | \ | | |/ / / ._\ |
| 18 | + | | | | , \| | / \ \ |
| 19 | + | |___| | |\ \ | |\ \ _\ \ |
| 20 | + |_____|_|_| \__|_| \_|____/ |
| 21 | + Welcome to Links version 0.9 (Burghmuirhead) |
| 22 | + links> |
| 23 | + |
| 24 | +You can now write an expression, and Links will show you the result. Note that |
| 25 | +in interactive mode, each expression must be terminated with a semicolon. |
| 26 | + |
| 27 | +.. code-block:: |
| 28 | +
|
| 29 | + links> 1 + 2 + 3;; |
| 30 | + 6 : Int |
| 31 | +
|
| 32 | +The Links REPL includes directives, which allow you to perform operations such |
| 33 | +as loading a file or showing the current type environment. To show all available |
| 34 | +directives, type ``@directives;``. |
| 35 | + |
| 36 | +A useful directive is to load in a Links file, which populates the environment |
| 37 | +with all functions defined in the file. To see this, create a file called |
| 38 | +``helloWorld.links``:: |
| 39 | + |
| 40 | + fun sayHello() { |
| 41 | + print("Hello, world!") |
| 42 | + } |
| 43 | + |
| 44 | +Now, from within a REPL, type ``@load "helloWorld.links";``, and then type |
| 45 | +``sayHello()``. You should see ``Hello, world!`` printed to the console:: |
| 46 | + |
| 47 | + _ _ __ _ _ __ ___ |
| 48 | + / | | | \ | | |/ / / ._\ |
| 49 | + | | | | , \| | / \ \ |
| 50 | + | |___| | |\ \ | |\ \ _\ \ |
| 51 | + |_____|_|_| \__|_| \_|____/ |
| 52 | + Welcome to Links version 0.9 (Burghmuirhead) |
| 53 | + links> @load "helloWorld.links"; |
| 54 | + () : () |
| 55 | + links> |
| 56 | + ...... sayHello(); |
| 57 | + Hello, world!() : () |
| 58 | + links> |
| 59 | + |
| 60 | +To exit the REPL, either press ``Ctrl-D`` or enter ``@quit;``. |
| 61 | + |
| 62 | +Running files |
| 63 | +------------- |
| 64 | + |
| 65 | +You are likely to want to run Links files from the command line, rather than |
| 66 | +from the REPL. Links files consist of a series of bindings, optionally followed |
| 67 | +by an expression. As an example, change ``helloWorld.links`` to:: |
| 68 | + |
| 69 | + fun sayHello() { |
| 70 | + print("Hello, world!") |
| 71 | + } |
| 72 | + sayHello() |
| 73 | + |
| 74 | +Now, you can run the file from the command line:: |
| 75 | + |
| 76 | + $ linx helloWorld.links |
| 77 | + Hello, world!() : () |
| 78 | + |
| 79 | +Running the examples |
| 80 | +-------------------- |
| 81 | + |
| 82 | +As described in :ref:`install`, the environment variable ``$OPAM_SWITCH_PREFIX`` |
| 83 | +points to the currently-active OPAM switch, and the example Links programs are |
| 84 | +installed into into ``$OPAM_SWITCH_PREFIX/share/links``. |
| 85 | + |
| 86 | +To run the examples, run the following command at a terminal:: |
| 87 | + |
| 88 | + $ linx -m --path=$OPAM_SWITCH_PREFIX/share/links/examples:$OPAM_SWITCH_PREFIX/share/links/examples/games $OPAM_SWITCH_PREFIX/share/links/examples/webserver/examples-nodb.links |
| 89 | + |
| 90 | +The ``-m`` flag enables modules, and the ``--path`` flag sets the path of where |
| 91 | +to look for Links files. |
| 92 | + |
| 93 | +You should then be able to run the (non-database) examples by visiting |
| 94 | +``http://localhost:8080`` in a web browser. |
| 95 | + |
| 96 | + |
| 97 | +Setting up the database |
| 98 | +----------------------- |
| 99 | + |
| 100 | +In this section, we will set up the database. We will concentrate on the |
| 101 | +``postgresql`` driver, but to use ``sqlite``, change ``postgresql`` to |
| 102 | +``sqlite``. |
| 103 | + |
| 104 | +If you have not yet installed the ``links-postgresql`` library, you will need to |
| 105 | +do so; see :ref:`install`. |
| 106 | + |
| 107 | +Configuration file |
| 108 | +~~~~~~~~~~~~~~~~~~ |
| 109 | + |
| 110 | +Next, we need to set up a configuration file which will contain the |
| 111 | +configuration options used to connect to the database. By default Links uses a |
| 112 | +configuration file located at ``$OPAM_SWITCH_PREFIX/etc/links/config``. Use the |
| 113 | +``--config=/custom/config/file`` flag to use a different configuration file. |
| 114 | + |
| 115 | +To use database examples you need to add the database driver and connection |
| 116 | +string to the configuration file:: |
| 117 | + |
| 118 | + database_driver=postgresql |
| 119 | + database_args=localhost:5432:fred:password123 |
| 120 | + |
| 121 | +This tells Links to use the ``postgresql`` database driver; to connect to the |
| 122 | +SQL server on ``localhost`` running on port ``5432```; and to connect as user |
| 123 | +``fred`` with password ``password123``. If the server is running on |
| 124 | +``localhost``, or authentication does not require a password, then these two |
| 125 | +arguments can be omitted. |
| 126 | + |
| 127 | +To use the above database string, the database user fred should exist. With |
| 128 | +Postgres, perhaps the easiest way to do this is to change ``fred`` to your Unix |
| 129 | +username, and thus no separate authentication is required. |
| 130 | + |
| 131 | +Creating databases |
| 132 | +~~~~~~~~~~~~~~~~~~ |
| 133 | + |
| 134 | +If you are using postgres and you have tied your username to the database as |
| 135 | +described above, then you can initialise the database using the scripts in |
| 136 | +``examples/dbsetup``:: |
| 137 | + |
| 138 | + cd examples/dbsetup |
| 139 | + ./createdbs |
| 140 | + ./populatedbs |
| 141 | + |
| 142 | +Otherwise, you can adapt them to your particular database set up. |
| 143 | + |
| 144 | +The PostgreSQL data dumps for the larger databases (citations and |
| 145 | +dictionary) are not included in the main repository, but can be obtained from a |
| 146 | +`separate repository`_. |
| 147 | + |
| 148 | +Running the database examples |
| 149 | +----------------------------- |
| 150 | + |
| 151 | +You should then be able to run the examples using the database:: |
| 152 | + |
| 153 | + $ linx -m --path=$OPAM_SWITCH_PREFIX/share/links/examples:$OPAM_SWITCH_PREFIX/share/links/examples/games $OPAM_SWITCH_PREFIX/share/links/examples/webserver/examples.links |
| 154 | + |
| 155 | +Adding the default examples path |
| 156 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 157 | + |
| 158 | +For convenience, you may wish to add the default examples path to your |
| 159 | +configuration file:: |
| 160 | + |
| 161 | + path=$OPAM_SWITCH_PREFIX/share/links/examples:$OPAM_SWITCH_PREFIX/share/links/examples/games:$OPAM_SWITCH_PREFIX/share/links/examples/dictionary |
| 162 | + |
| 163 | +You can then simply issue the following command to run the examples:: |
| 164 | + |
| 165 | + $ linx -m $OPAM_SWITCH_PREFIX/share/links/examples/webserver/examples.links |
| 166 | + |
| 167 | +.. _separate repository: http://www.github.com/links-lang/links-data |
0 commit comments