# 8.2 Modules and projects

> When a program grows, it is split into files: every file is a **module**, and a module uses what another one makes **public** by importing it.

Web page: https://pseudolearn.app/en/spec/programas/modulos

> **In one sentence**
>
> When a program grows, it is split into files: every file is a **module**, and a module uses what another one makes **public** by importing it.

## 8.2.1 How it works

A **project** groups **packages**. Every package has a name and a root folder, and every file inside that root is a module. The module's name is its path inside the root with dots instead of slashes.

**Figure 8.2-1: A package with two modules**

A package called tienda with the module principal, which is the entry and holds the algorithm, and the module formas.geometria, with the public subroutine AreaCirculo and the private Ayudante. An import arrow goes from principal to geometria.

A package with two modules. `principal` imports `formas.geometria` and uses its public subprogram.

A package can be:

- an **application**: it declares one or more **entries**, modules with an algorithm; one is chosen when running;
- a **library**: it has no entries or algorithms; it only offers units to other packages.

Only the entry module contains an algorithm. The others contain subprograms, classes and the remaining units.

Failures in the project configuration —an entry that does not exist, a module outside every root, a cycle between packages— are not code diagnostics: they are **project errors**, with a stable code such as `entryNotFound` or `packageCycle` and the affected path.

## 8.2.2 Visibility

Every top-level unit is **private** to its module unless it carries the `Public` prefix. Only what is public can be imported.

### Program: formas/geometria.psl

```pseudolearn
Public Subroutine AreaCirculo(radio As Real) As Real
  Return 3.14159 * radio * radio;
EndSubroutine

Subroutine Ayudante()
EndSubroutine
```

*This program is not run.*

## 8.2.3 Importing

`Import` names a module or a symbol by its path, starting with the package name. An imported module is used through its last segment, or through the alias given by `As`:

### Program: principal.psl

```pseudolearn
Import tienda.formas.geometria;

Algorithm Principal
  Write geometria.AreaCirculo(2.0);
EndAlgorithm
```

*This program is not run.*

With `Export`, a module offers a public symbol of another module as its own, without creating a local name. Importing and exporting **run nothing**: an unused import does not change the result.

> **Limit**
>
> - There are no wildcards: a name always follows a dot in the path.
> - The import graph cannot have cycles: a cycle is `importCycle` and no body is analysed.
> - Two equal local names with different origins are `importNameCollision`.
> - A public signature cannot name a private type of the module: `privateTypeInPublicSignature`.

> **Common mistakes**
>
> In a loose text, without a project, `Import` and `Public` make no sense:
>
> ### Program: Import outside a project
>
> ```pseudolearn
> Import geometria;
> Algorithm Suelto
>   Write 1;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `moduleSyntaxOutsideProject` (error, 1:1): Import, export and unit visibility are only allowed inside a project
>
> *The program finishes with an error.*

**In short**
- Every file is a module; modules form packages and packages, a project.
- Only what is marked `Public` can be imported.
- `Import` uses paths that start with the package; `As` gives an alias.
- Import cycles are not allowed.

**See also**
- [8.1 Structure of a program](https://pseudolearn.app/en/spec/programas/estructura.md)
- [7.1 Declaring subprograms](https://pseudolearn.app/en/spec/subprogramas/declaracion.md)
