Perl - ArchWiki

Jump to content

# Perl

From ArchWiki

Related articles

- List of programming languages
- Perl package guidelines
- Perl Policy
- mod_perl

From Wikipedia:

  Perl is a high-level, general-purpose, interpreted, dynamic programming language.
  Perl borrows features from other programming languages including C, sh, AWK, and sed. It provides text processing facilities without the arbitrary data-length limits of many contemporary Unix command line tools.
  Perl gained widespread popularity in the mid-1990s as a CGI scripting language, in part due to its powerful regular expression and string parsing abilities.

## Installation

Install the perl package.

## Commands

The Perl language interpreter:

    $ perl

Perl bug reporting:

    $ perlbug

Lookup the Perl documentation in POD format:

    $ perldoc

Send the Perl authors and maintainers a thank you message:

    $ perlthanks

## Package management

There are many ways to install Perl packages on Arch Linux. The majority of Perl modules are distributed over CPAN, the Comprehensive Perl Authors Network.

### Arch repositories

A number of popular CPAN modules are available in the official repositories and AUR. This is the preferred way to install system-wide packages, and the only method officially supported on Arch Linux.

### CPAN packages

Since Perl packages in the Arch repositories can be out-of-date or missing, one may be tempted to install them with installers like cpan or cpanm as root. However, this is not recommended because it can lead to untracked modules conflicting with the system packages.

One may also write PKGBUILDs and install them with makepkg/pacman, though tedious.

A safer approach is to use perl-local-lib, which lets you install modules locally (by default in `~/perl5`) while still using the system perl. `local::lib` sets up environment variables that make perl look in a particular directory for libraries, and install modules there.

Note There are also tools that generate `PKGBUILD`s for CPAN modules: see Creating packages#PKGBUILD generators.

### CPAN installers

Installers download, build and install Perl modules from CPAN.

#### cpanminus

cpanminus is a lightweight, zero configuration module installer that supports `local::lib` and perlbrew/plenv. It is typically the recommended installer over the more verbose cpan tool [1] [2].

By default, it installs modules to the site_perl directories when running as root (discouraged), otherwise to `~/perl5` [3].

##### Usage examples

Install modules:

    $ cpanm Regexp::Debugger

Uninstall modules:

    $ cpanm --uninstall Regexp::Debugger

Check outdated modules (requires cpan-outdated):

    $ cpanm App::cpanoutdated
    $ cpan-outdated -p

Update modules:

    $ cpan-outdated | cpanm

See the cpanm documentation for more examples.

#### CPAN.pm

`CPAN.pm` is a Perl core module. It can be used interactively from the shell or in Perl scripts.

##### Configuring cpan

Before first use, the module needs to be configured. This is done interactively from the shell with:

    $ cpan

    CPAN.pm requires configuration, but most of it can be done automatically.
    If you answer 'no' below, you will enter an interactive dialog for each
    configuration option instead.

    Would you like to configure as much as possible automatically? [yes]

Automated configuration will suit most users. Answering yes, the configuration will continue with:

    To install modules, you need to configure a local Perl library directory or
    escalate your privileges. CPAN can help you by bootstrapping the local::lib
    module or by configuring itself to use 'sudo' (if available). You may also
    resolve this problem manually if you need to customize your setup.

    What approach do you want?  (Choose 'local::lib', 'sudo' or 'manual')
     [local::lib]

To install modules in your home directory, choose `local::lib`, which installs additional modules. To install them system-wide, choose `sudo` (discouraged), which ends the configuration:

    Autoconfiguration complete.

    commit: wrote '/home/username/.cpan/CPAN/MyConfig.pm'

Choosing not to use automated configuration allows the user to set `cpan` options interactively in the shell. The table below shows some option names with a brief description and default value. More detailed information is displayed for each option during configuration.

Name | Description | Default
--- | --- | ---
cpan_home | CPAN build and cache directory | $HOME/.cpan
keep_source_where | Download target directory | $HOME/.cpan/sources
build_dir | Build process directory | $HOME/.cpan/build
prefs_dir | Customizable modules options directory | $HOME/.cpan/prefs
build_cache | Cache size for build directory | 100MB
cleanup_after_install | Remove build directory after successful install | No
shell | Preferred shell | /bin/bash
halt_on_failure | Halt on failure | No
colorize_output | Turn on colored output | No
histfile | History file location | $HOME/.cpan/histfile
histsize | History file size | 100 lines

The configuration file `$HOME/.cpan/CPAN/MyConfig.pm` can be edited with your text editor of choice.

##### Usage examples

To install modules, pass them as parameters to `cpan`:

    $ cpan Acme::MetaSyntactic

The following examples are in the `cpan` interactive shell, started with:

    $ cpan

Display module information:

    cpan[1]> m Acme::MetaSyntactic

    Reading '/home/username/.cpan/Metadata'
      Database was generated on Fri, 08 Dec 2017 02:17:03 GMT
    Module id = Acme::MetaSyntactic
        CPAN_USERID  BOOK (Philippe Bruhat (BooK) <book@cpan.org>)
        CPAN_VERSION 1.014
        CPAN_FILE    B/BO/BOOK/Acme-MetaSyntactic-1.014.tar.gz
        INST_FILE    (not installed)

View the `README`:

    cpan[2]> readme Acme::MetaSyntactic

    Acme::MetaSyntactic - Themed metasyntactic variables

    DESCRIPTION

    When writing code examples, it's always easy at the beginning:

       my $foo = "bar";
       $foo .= "baz";   # barbaz
    ...

Install a module:

    cpan[3]> install Acme::MetaSyntactic

Re-run configuration:

    cpan[1]> o conf init

## Perl development environment

perlbrew and plenvAUR automate the installation and management of multiple perl versions under your home directory, allowing you to install modules locally without conflicting with the system perl.

See their documentation [4] [5] for more details.

## Development tools

See also List of recommended Perl modules.

### REPLs

- Reply — Read, eval, print, loop, yay!

  https://metacpan.org/pod/reply || perl-replyAUR

### Debugging

- Data::Printer — Colored & full-featured pretty print of Perl data structures and objects.

  https://metacpan.org/pod/Data::Printer || perl-data-printerAUR

- Devel::NYTProf — Powerful fast feature-rich Perl source code profiler.

  https://metacpan.org/pod/Devel::NYTProf || perl-devel-nytprofAUR

### Linters

- Perl::Critic — Command-line interface to critique Perl source.

  https://metacpan.org/pod/perlcritic || perl-perl-critic

### Formatters

- Perl::Tidy — A perl script indenter and reformatter.

  https://metacpan.org/pod/perltidy || perl-tidy

### Testing

- Devel::Cover — Code coverage metrics for Perl.

  https://metacpan.org/pod/Devel::Cover || perl-devel-cover

- Test2::Suite — Distribution with a rich set of tools built upon the Test2 framework.

  https://metacpan.org/pod/Test2::Suite || perl-test-simpleAUR

### Widget bindings

- Gtk2 — Perl interface to the 2.x series of the Gimp Toolkit library.

  https://gtk2-perl.sourceforge.net/doc/pod/Gtk2.html || gtk2-perlAUR

- Gtk3 — Perl interface to the 3.x series of the GTK+ toolkit.

  https://metacpan.org/pod/Gtk3 || perl-gtk3

- Prima — A Perl graphic toolkit.

  https://metacpan.org/pod/Prima || perl-primaAUR

- Tk — A graphical user interface toolkit for Perl.

  https://metacpan.org/pod/Tk || perl-tk

- Wx — Interface to the wxWidgets cross-platform GUI toolkit.

  https://metacpan.org/pod/Wx || perl-wxAUR

## IDE support

### Development in JetBrains IDE

If you are using a JetBrains IDE, for example IntelliJ Idea, install perl-bundle-camelcadeAUR. Then install Perl plugin. Then go to the Settings > Languages & Frameworks > Perl5. In the Perl 5 Interpreter field select Add System Perl. Now you can make a run/debug configuration for your project and start debugging.

## Troubleshooting

### Module not found after Perl version update

After upgrading perl (e.g. from 5.40 to 5.42), a Perl application may fail with an error like:

    Can't locate Foo/Bar.pm in @INC ...

This can happen when the dependency is an XS module built against the previous perl, so it must be rebuilt because the perl package now uses a versioned path for compiled modules [6].

This usually affects Perl modules installed from AUR and CPAN installers, since Perl packages in the official repositories are rebuilt with major Perl releases. See AUR#Updating packages.

Note When upgrading perl, a pacman hook may warn about installed Perl modules that need rebuilding.

## See also

### Perl

- Official Perl website
- Perl documentation

### CPAN

- Official CPAN website
- Using CPAN with Perl

### Tutorials

- Perl Tutorial Hub
- Online Perl Tutorials

Category:

- Programming languages

Perl
Add topic