How to set and get PERL5LIB environment variable on Github Action
Tags: github, githubaction, perl, beginners
Published: 2022-12-14 07:41:22

How to set and get PERL5LIB environment variables on Github Action.

## Linux, Mac

```
      - name: Set PERL5LIB
        run: echo "PERL5LIB=$HOME/mylib/lib/perl5:$HOME/mylib/lib/perl5/$(perl -MConfig -e 'print $Config{archname}')" >> $GITHUB_ENV
      - name: Show PERL5LIB
        run: echo $PERL5LIB
```

An example:

```
name: linux-ubuntu-latest

on:
  push:
    branches:
      - '*'
    tags-ignore:
      - '*'
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Perl version
        run: perl -V
      - name: Perl config
        run: perl -MConfig -MData::Dumper -e 'local $Data::Dumper::Sortkeys = 1;warn Dumper \%Config;'
      - name: Set PERL5LIB
        run: echo "PERL5LIB=$HOME/mylib/lib/perl5:$HOME/mylib/lib/perl5/$(perl -MConfig -e 'print $Config{archname}')" >> $GITHUB_ENV
      - name: Show PERL5LIB
        run: echo $PERL5LIB
      - name: Download cpanm
        run: perl -0777 -Mstrict -Mwarnings -MIO::Socket::INET -e 'my $socket = IO::Socket::INET->new(  Proto => qq(tcp),  PeerAddr => qq(cpanmin.us),  PeerPort => 80,) or die $!;print $socket join qq(\r\n),  qq(GET / HTTP/1.1),  qq(Connection:close),  qq(Host:cpanmin.us),  qq(\r\n);my $res = <$socket>;$res =~ s[^HTTP/1\.1 200 OK\r\n.*?\r\n\r\n][]s or die $res;open my $out, qq(>), qq(cpanm) or die $!;print $out $res'
      - name: cpanm installs dependent modules
        run: perl cpanm -v -n -L ~/mylib --installdeps . --configure-args="--meta"
      - run: perl Makefile.PL
      - run: make
      - run: make disttest

```

## Windows/pwsh

```
      - name: Set PERL5LIB
        run: echo "PERL5LIB=$HOME/mylib/lib/perl5;$HOME/mylib/lib/perl5/$(perl -MConfig -e 'print $Config{archname}')" >> $env:GITHUB_ENV
      - name: Show PERL5LIB
        run: echo $env:PERL5LIB
```

An example:

```
name: windows-2019

on:
  push:
    branches:
      - '*'
    tags-ignore:
      - '*'
  pull_request:

jobs:
  perl:
    runs-on: windows-2019
    steps:
      - uses: actions/checkout@v3
      - name: Perl version
        run: perl -V
      - name: Perl config
        run: perl -MConfig -MData::Dumper -e 'local $Data::Dumper::Sortkeys = 1;warn Dumper \%Config;'
      - name: Set PERL5LIB
        run: echo "PERL5LIB=$HOME/mylib/lib/perl5;$HOME/mylib/lib/perl5/$(perl -MConfig -e 'print $Config{archname}')" >> $env:GITHUB_ENV
      - name: Show PERL5LIB
        run: echo $env:PERL5LIB
      - name: Download cpanm
        run: perl -0777 -Mstrict -Mwarnings -MIO::Socket::INET -e 'my $socket = IO::Socket::INET->new(  Proto => qq(tcp),  PeerAddr => qq(cpanmin.us),  PeerPort => 80,) or die $!;print $socket join qq(\r\n),  qq(GET / HTTP/1.1),  qq(Connection:close),  qq(Host:cpanmin.us),  qq(\r\n);my $res = <$socket>;$res =~ s[^HTTP/1\.1 200 OK\r\n.*?\r\n\r\n][]s or die $res;open my $out, qq(>), qq(cpanm) or die $!;print $out $res'
      - name: cpanm installs dependent modules
        run: perl cpanm -v -n -L ~/mylib --installdeps . --configure-args="--meta"
      - run: perl Makefile.PL
      - run: gmake
      - run: gmake disttest

```

## Note

Set a environment variable.

```
# Linux, Mac
echo "PERL5LIB=Foo" >> $GITHUB_ENV

# Linux, Mac
echo "PERL5LIB=Foo" >> $env:GITHUB_ENV
```

Get a environment variable.

```
# Linux, Mac
echo $PERL5LIB

# Windows
echo $env:PERL5LIB
```

This is needed to be shown at the next C<code>run</code>

Path separator is <code>:</code> on Linux, Mac, <code>;</code> on Windows.

## For Beginners

<code>PERL5LIB</code> environment variable is the path that Perl search for modules.