Install Perl on macOS
The system Perl
Every Mac has /usr/bin/perl. It is older than the current release, and Apple keeps it only because some system scripts use it:
/usr/bin/perl -v
This is perl 5, version 30, subversion 3 (v5.30.3) built for darwin-thread-multi-2level
Do not install modules into that copy. It belongs to the operating system, an update may replace it, and mixing your modules with Apple's can break system tools. Install a separate Perl and leave this one alone.
Homebrew perl
Homebrew installs a current Perl into its own prefix. The formula is keg-only, which means it will not overwrite the system binary, so you decide when to use it:
brew install perl
Compiling modules needs the command line developer tools. If you have never installed them, one command sets them up:
xcode-select --install
Homebrew prints the exact lines to add to your shell profile. They put its bin folder in front of /usr/bin, so perl resolves to the new one:
export PATH="$(brew --prefix perl)/bin:$PATH"
On Apple Silicon the prefix is /opt/homebrew; on Intel it is /usr/local. Using brew --prefix avoids hardcoding either path.
Path and version
Open a new terminal and check that the shell finds the Homebrew copy, not the system one:
which perl
/opt/homebrew/opt/perl/bin/perl
perl -v
This is perl 5, version 40, subversion 0 (v5.40.0) built for darwin-thread-multi-2level
Install cpanm with Homebrew as well, so both tools come from the same place:
brew install cpanminus
Then a module install is the usual cpanm Text::CSV. Because it installs into the Homebrew Perl, the system copy stays clean.
perlbrew for several versions
When you need to test against two releases, perlbrew keeps them side by side in your home folder and switches with one command:
curl -L https://install.perlbrew.pl | bash
perlbrew install perl-5.40.0
perlbrew switch perl-5.40.0
The installer appends a line to ~/.bash_profile or ~/.zshrc, depending on your shell. Open a new terminal before the first build, or the perlbrew command will not be found.
Remember that #!/usr/bin/perl ignores all of this. Use #!/usr/bin/env perl in scripts that should follow your shell's choice, and keep the absolute path only for scripts that must run under the system Perl.
Next: Install Perl on FreeBSD
Article author: Arthur Isaev