Skip to content

Running another file with require

require 'config.nvs' runs another file at the place where you write it. The value of require has the type mixed. Text converted from a mixed value is tainted, so as array<string> gives an array<tainted string>.

require

require runs another file right where you write it, as part of the program that is already running.

The other file's own lines run in order at that point, every time your program reaches the require. There is no once-only version. A require is also a value: whatever the other file hands back with return at its top level is what you get, and you convert it with as to the type you expect. A file that returns nothing hands back 1. The path is written relative to the folder holding the file you wrote the require in.

Good to know: one kind of name crosses between the two files and one does not. Every class, interface, enum and type alias the other file declares is yours to use, as if you had typed it in. Variables are not shared, so the other file's $name is its own and yours stays yours. To hand something across, put it in a class.

Keep settings in their own file

<?nvs
// A settings file is a file that returns a value. `require` runs it and hands
// you back what it returned, so nothing here has to read or parse a file.
// The value arrives as `mixed`, which is why it is converted with `as`.
array<tainted string> $settings = (require 'parts/settings.nvs') as array<string>;
echo 'connecting to ', $settings['host'], ':', $settings['port'], "\n";
echo 'giving up after ', $settings['timeout'], " seconds\n";
Output
connecting to db.example.test:5432
giving up after 30 seconds

Share a class between files

<?nvs
// A class declared in another file is yours as soon as you require that file.
// Novis reads it as if you had typed the class in here, so nothing has to be
// registered and nothing has to be imported.
require 'parts/order-label.nvs';
echo OrderLabel::line(1042, 'Alice Mercier', 'Lyon'), "\n";
echo OrderLabel::line(1043, 'Bo Nilsson', 'Malmo'), "\n";
Output
Order #1042 - Alice Mercier, Lyon
Order #1043 - Bo Nilsson, Malmo

Print a section of a report twice

<?nvs
// `require` runs the file each time your program reaches it, so one file prints
// each section of the report. The program passes the section data in the
// `Section` class from `parts/section.nvs`. Variables are not shared between files.
echo "Support, this morning\n\n";
Section::$title = 'Open tickets';
Section::$rows = ['#41 printer jam', '#42 badge reader'];
require 'parts/section.nvs';
Section::$title = 'Waiting on a customer';
Section::$rows = ['#38 refund address'];
require 'parts/section.nvs';
Output
Support, this morning
Open tickets
#41 printer jam
#42 badge reader
Waiting on a customer
#38 refund address

Details

The path is read while the program compiles. If no file is at that path, the program does not compile (E0311). The path may be text in quotes, a class constant, or several of these joined with ..

A path built from a variable is not read while compiling. When the program reaches that require, it throws a RuntimeError, even if the file exists.

The other file's top-level code is checked on its own. Its variables are new variables, even when a name is the same as one of yours: after the require, your $x still has its old value.

include, include_once and require_once do not compile (E0221). The message names require. A file of declarations that must be read once is found through autoload, which never reads a file twice.

  • Do use require for a file that returns a value, such as settings.
  • Do convert the result with as right away.
  • Don’t use require for a file that only declares classes. Use autoload, which reads it once.
  • Don’t expect a variable from the other file. Return the value from that file.