> For the complete documentation index, see [llms.txt](https://clinical-genomics.gitbook.io/project-mip/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://clinical-genomics.gitbook.io/project-mip/develop/documentation/api/subroutines.md).

# Subroutines

There are two main type of sub routines in MIP. 1. Sub routines that are imported/exported 2. Utility sub routines that only resides in the script or module they are created. These are named using an underscore in the beginning of the sub routine name i.e. `sub _a_utility_sub`

## Template

A template for sub routines are found in the [code dir](https://github.com/Clinical-Genomics/MIP/tree/master/templates/code/).

The sub routine consists of four sections:

1. A documentation part with a mandatory header:

   ```perl
   ## Function : Describe the sub routine function here
   ## Returns  : Name of variables returned. If none leave blank
   ## Arguments: $arrays_ref => Array ref description {REF}
   ##          : $hash_href  => Hash ref description {REF}
   ##          : $scalar     => Scalar description
   ```
2. Initilization of parameters:

   ```perl
   my ($arg_href) = @_;  #Always start with unpacking the parameters array

    ## Flatten argument(s)
    my $arrays_ref;  # Parameters without a default value
    my $hash_href;

    ## Default(s)  # This is for parameters with a default value
    my $scalar;
   ```
3. Checking the supplied parameters:

   ```perl
   my $tmpl = {
        arrays_ref => {
            default     => [], # Empty array ref
            defined     => 1, # Must be defined when passed
            required    => 1, # Must be supplied when passed
            store       => \$arrays_ref, # Where to store the parameter
            strict_type => 1, # Require correct type
        },
        hash_href => {
            default     => {},
            defined     => 1,
            required    => 1,
            store       => \$hash_href,
            strict_type => 1,
        },
        scalar => {
            allow       => qr/ ^\d+$ /sxm, # Set allowed value
            default     => 1,
            store       => \$scalar,
            strict_type => 1,
        },
    };
   check( $tmpl, $arg_href, 1 ) or croak q{Could not parse arguments!}; # Check parameters according to template
   ```
4. The main part of the sub routine that actually does something.
5. Finally always end with a return statement

   ```perl
   return;
   ```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://clinical-genomics.gitbook.io/project-mip/develop/documentation/api/subroutines.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
