#  Installer Perl



macOS Catalina est livré avec Perl:

```bash
$ which perl
/usr/bin/perl

$ perl -v

This is perl 5, version 18, subversion 4 (v5.18.4) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)
```



### Homebrew:

On peut installer une version plus récente de Perl avec Homebrew:

```bash
$ brew install perl
```

```bash
$ brew info perl
zsh: correct 'perl' to 'perl5' [nyae]? n

perl: stable 5.30.0 (bottled), HEAD
Highly capable, feature-rich programming language
https://www.perl.org/
/usr/local/Cellar/perl/5.30.0 (2,440 files, 61.8MB) *
  Poured from bottle on 2019-08-04 at 13:32:46
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/perl.rb
==> Options
--HEAD
	Install HEAD version
==> Caveats
By default non-brewed cpan modules are installed to the Cellar. If you wish
for your modules to persist across updates we recommend using `local::lib`.

You can set that up like this:
  PERL_MM_OPT="INSTALL_BASE=$HOME/perl5" cpan local::lib
  echo 'eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib=$HOME/perl5)"' >> ~/.zshrc
```

```bash
$ which perl
/usr/local/bin/perl

$ perl -v

This is perl 5, version 30, subversion 0 (v5.30.0) built for darwin-thread-multi-2level

Copyright 1987-2019, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
```

#### cpan:

**cpan** gère les modules Perl.

```bash
$ cpan
Loading internal logger. Log::Log4perl recommended for better logging

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] yes


Autoconfiguration complete.

commit: wrote '/Users/bruno/.cpan/CPAN/MyConfig.pm'

You can re-run configuration any time with 'o conf init' in the CPAN shell
Terminal does not support AddHistory.

To fix enter>  install Term::ReadLine::Perl


cpan shell -- CPAN exploration and modules installation (v2.22)
Enter 'h' for help.
```

Sans argument, la commande **cpan** ouvre un shell:

```bash
$ cpan
Loading internal logger. Log::Log4perl recommended for better logging

cpan shell -- CPAN exploration and modules installation (v2.27)
Enter 'h' for help.

cpan[1]> h
```

```bash
cpan[1]> h

Display Information                                                  (ver 2.27)
 command  argument          description
 a,b,d,m  WORD or /REGEXP/  about authors, bundles, distributions, modules
 i        WORD or /REGEXP/  about any of the above
 ls       AUTHOR or GLOB    about files in the author's directory
    (with WORD being a module, bundle or author name or a distribution
    name of the form AUTHOR/DISTRIBUTION)

Download, Test, Make, Install...
 get      download                     clean    make clean
 make     make (implies get)           look     open subshell in dist directory
 test     make test (implies make)     readme   display these README files
 install  make install (implies test)  perldoc  display POD documentation

Upgrade installed modules
 r        WORDs or /REGEXP/ or NONE    report updates for some/matching/all
 upgrade  WORDs or /REGEXP/ or NONE    upgrade some/matching/all modules

Pragmas
 force  CMD    try hard to do command  fforce CMD    try harder
 notest CMD    skip testing

Other
 h,?           display this menu       ! perl-code   eval a perl command
 o conf [opt]  set and query options   q             quit the cpan shell
 reload cpan   load CPAN.pm again      reload index  load newer indices
 autobundle    Snapshot                recent        latest CPAN uploads
```

La commande **cpan** peut aussi s'utiliser avec arguments:



Avec <u>Perl installé via homebrew</u>, les modules sont installés dans Cellar (`Installing /usr/local/Cellar/perl/5.30.0/lib/perl5/site_perl/5.30.0/CPAN/DistnameInfo.pm`). Ils sont donc perdu à chaque mise-à-jour de Perl.

Il est donc conseillé d'utiliser `local::lib`

```bash
PERL_MM_OPT="INSTALL_BASE=$HOME/perl5" cpan local::lib
echo 'eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib=$HOME/perl5)"' >> ~/.zshrc

# les dossiers suivants sont crées:
#		$HOME/perl5/bin/
#		$HOME/perl5/lib/
#		
# à côté de $HOME/perl5/perlbrew/
```

Les modules sont alors installés ici `Installing /Users/bruno/perl5/lib/perl5/Log/Log4perl.pm`



##### Info sur un module:  `cpan -D <module>`

```bash
$ cpan -D CPAN::DistnameInfo
Reading '/Users/bruno/.cpan/Metadata'
  Database was generated on Tue, 17 Dec 2019 07:29:02 GMT
CPAN::DistnameInfo
-------------------------------------------------------------------------
	CPAN: Module::CoreList loaded ok (v5.20190522)
(no description)
	G/GB/GBARR/CPAN-DistnameInfo-0.12.tar.gz
	/usr/local/Cellar/perl/5.30.0/lib/perl5/site_perl/5.30.0/CPAN/DistnameInfo.pm
	Installed: 0.12
	CPAN:      0.12  up to date
	Graham Barr (GBARR)
	gbarr@pobox.com
```



##### Liste des modules installés: `cpan -l`

```bash
$ cpan -l
Spiffy	0.46
YAML	1.29
CPAN::DistnameInfo	0.12
App::pmuninstall	0.30
Test::YAML	1.07
```



##### Désintaller un module:

Il n'y a pas de commande prévue pour ça. 

On peut installer [pmuninstall](https://github.com/xaicron/pm-uninstall).

```bash
$ cpan -i App::pmuninstall
```

Puis pour désinstaller:

```bash
$ pm-uninstall -v App::cpnaminus
```



##### Modules mis-à-jour:

On peut installer [cpanoutdated](https://github.com/tokuhirom/cpan-outdated)

```bash
$ cpan -i App::cpanoutdated
```

et voir les modules 'outdated'

```bash
$ cpan-outdated -p
Compress::Raw::Bzip2
Compress::Raw::Zlib
Compress::Zlib
DB_File

$ cpan-outdated
P/PM/PMQS/Compress-Raw-Bzip2-2.093.tar.gz
P/PM/PMQS/Compress-Raw-Zlib-2.093.tar.gz
P/PM/PMQS/IO-Compress-2.093.tar.gz
P/PM/PMQS/DB_File-1.852.tar.gz
```



Il est recommandé d'installer les modules suivants:

- Term::ReadLine::Perl (réclamé par le shell cpan)

- CPAN::DistnameInfo (réclamé par l'installeur de module)
- Log::Log4perl



### PerlBrew:

On peut installer d'autres versions de Perl avec [PerlBrew](https://perlbrew.pl).

Il faut au préalable installer **berkeley-db**, sans quoi il y aura une erreur à l'installer de Perl avec perlbrew.

```bash
$ brew install berkeley-db
```

#### Installation de perlbrew:

```bash
$ curl -L https://install.perlbrew.pl | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   170  100   170    0     0    245      0 --:--:-- --:--:-- --:--:--   245
100  1554  100  1554    0     0   1935      0 --:--:-- --:--:-- --:--:--  1935

## Download the latest perlbrew

## Installing perlbrew
Using Perl </usr/bin/perl>
perlbrew is installed: ~/perl5/perlbrew/bin/perlbrew

perlbrew root (~/perl5/perlbrew) is initialized.

Append the following piece of code to the end of your ~/.bash_profile and start a
new shell, perlbrew should be up and fully functional from there:

    source ~/perl5/perlbrew/etc/bashrc

Simply run `perlbrew` for usage details.

Happy brewing!

## Installing patchperl

## Done.
```

Pour zsh, on ajoute la ligne suivant à `.zshenv`

```bash
source /Users/bruno/perl5/perlbrew/etc/bashrc
```



##### Puis on installe la version de Perl que l'on veux: 

```bash
# Installer la dernière version de Perl:
$ perlbrew install [options] stable

# Installer une version précise de Perl:
$ perlbrew install [options] 5.x.xx

```

##### Liste des versions de Perl installées (dans $PERLBREW_ROOT):

```bash
$ perlbrew list
  perl-5.30.1
```

##### On choisit la version que l'on veut utiliser:

```bash
$ perlbrew use 5.30.1
--------------------------------------------------------------------------------
WARNING: zsh perlbrew sub-shell is not working on Mac OSX Lion.

It is known that on MacOS Lion, zsh always resets the value of PATH on launching
a sub-shell. Effectively nullify the changes required by perlbrew sub-shell. You
may `echo $PATH` to examine it and if you see perlbrew related paths are in the
end, instead of in the beginning, you are unfortunate.

You are advised to include the following line to your ~/.zshenv as a better
way to work with perlbrew:

    source /Users/bruno/perl5/perlbrew/etc/bashrc

--------------------------------------------------------------------------------

A sub-shell is launched with 5.30.1 as the activated perl. Run 'exit' to finish it.

silverbook%
```

Pour bash, on ajoute `source ~/perl5/perlbrew/etc/bashrc` au *~/.bash_profile*.

Pour zsh, on ajoute `source /Users/bruno/perl5/perlbrew/etc/bashrc` au *~/.zshenv*.



##### Choisir la version de Perl dans le shell courant:


```bash
# perlbrew use [perl-<version> | <version> | <name>]

$ perlbrew use 5.30.1

$ which perl
/Users/bruno/perl5/perlbrew/perls/perl-5.30.1/bin/perl

$ perl -v

This is perl 5, version 30, subversion 1 (v5.30.1) built for darwin-2level
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2019, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
```

##### Afficher la version courante de Perl:

```bash
$ perlbrew use
Currently using perl-5.30.1
```

##### Changer de version de Perl pour le shell courant et les futures sessions:

```bash
# perlbrew switch [ <name> ]

$ perlbrew switch perl-5.30.1

# sans paramètre, affiche la version courante
$ perlbrew switch
Currently switched to perl-5.30.1
```

##### Désactiver temporairement perlbrew (dans le shell courant):

```bash
# et reactiver le Perl par défaut
$ perlbrew off
```

##### Désactiver perlbrew:

```bash
# et reactiver le Perl par défaut
$ perlbrew switch-off

# pour réactiver perlbrew
$ perlbrew switch
```

##### Créer un alias:

```bash
$ perlbrew alias [-f] create <name> <alias>

$ perlbrew alias [-f] rename <old_alias> <new_alias>

$ perlbrew alias delete <alias>
```

##### Exécuter une commande pour chaque version de Perl installée:

```bash
$ perlbrew exec perl -e 'print "Hello from $]\n"'
$ perlbrew exec --with perl-5.12,perl-5.12-debug,perl-5.14.2
```

##### Variables d'environnement:

```bash
$ perlbrew env
unset PERLBREW_MANPATH
export PERLBREW_PATH="/Users/bruno/perl5/perlbrew/bin"
unset PERLBREW_PERL
export PERLBREW_ROOT="/Users/bruno/perl5/perlbrew"
export PERLBREW_VERSION="0.87"
```

##### Installer cpanm:

```bash
$ perlbrew install-cpanm
```

##### Mettre à jour perlbrew:

```bash
$ perlbrew self-upgrade
```

##### Nettoyer les dossier d'installation et les archives:

```bash
$ perlbrew clean
Removing /Users/bruno/perl5/perlbrew/build/perl-5.30.1
Removing /Users/bruno/perl5/perlbrew/dists/perl-5.30.1.tar.gz

Done
```

##### Afficher la version de perlbrew:

```bash
$ perlbrew version
/Users/bruno/perl5/perlbrew/bin/perlbrew  - App::perlbrew/0.87
```

##### Mettre à jour Perl:

```bash
# met à jour les versions mineurs (5.14.0 -> 5.14.2)
$ perlbrew upgrade-perl
```

##### Télécharger Perl:

```bash
# télécharger l'archive Perl
$ perlbrew download perl-5.17.3
```

##### Liste des modules:

```bash
$ perlbrew list-modules
Modern::Perl
```

##### Cloner les modules entre 2 versions de Perl:

```bash
# perlbrew clone-modules <src_version> <dst_version>

$ perlbrew clone-modules 5.26.1 5.27.7
```



#### Installer des modules:

Avec perlbrew, on utilise **cpanm**:

```bash
$ cpanm Modern::Perl
--> Working on Modern::Perl
Fetching http://www.cpan.org/authors/id/C/CH/CHROMATIC/Modern-Perl-1.20190727.tar.gz ... OK
Configuring Modern-Perl-1.20190727 ... OK
Building and testing Modern-Perl-1.20190727 ... OK
Successfully installed Modern-Perl-1.20190727
1 distribution installed
```

Les modules sont installés ici:

```
/System/Volumes/Data/Users/bruno/perl5/perlbrew/perls/perl-5.30.1/lib/site_perl/5.30.1/darwin-2level/.meta/Modern-Perl-1.20190727
/System/Volumes/Data/Users/bruno/.cpanm/work/1576489302.28593/Modern-Perl-1.20190727
```

##### Mise-à-jour de cpanm:

```bash
$ cpanm --self-upgrade
It appears your cpanm executable was installed via `perlbrew install-cpanm`.
cpanm --self-upgrade won't upgrade the version of cpanm you're running.

Run the following command to get it upgraded.

  perlbrew install-cpanm
```

##### N° de version:

```bash
$ cpanm --V
cpanm (App::cpanminus) 1.7044 on perl 5.030000 built for darwin-thread-multi-2level
Work directory is /Users/bruno/.cpanm/work/1576565969.69495
You have make /usr/bin/make
You have /usr/local/bin/wget
You have /usr/bin/tar: bsdtar 3.3.2 - libarchive 3.3.2 zlib/1.2.11 liblzma/5.0.5 bz2lib/1.0.6
You have /usr/bin/unzip
```

