DEV Community

Yuki Kimoto
Yuki Kimoto

Posted on

How to set and get PERL5LIB environment variable on Github Action

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Note

Set a environment variable.

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

# Linux, Mac
echo "PERL5LIB=Foo" >> $env:GITHUB_ENV
Enter fullscreen mode Exit fullscreen mode

Get a environment variable.

# Linux, Mac
echo $PERL5LIB

# Windows
echo $env:PERL5LIB
Enter fullscreen mode Exit fullscreen mode

This is needed to be shown at the next Crun

Path separator is : on Linux, Mac, ; on Windows.

For Beginners

PERL5LIB environment variable is the path that Perl search for modules.

Top comments (4)

Collapse
 
szabgab profile image
Gabor Szabo

Why not use the env key?

Collapse
 
yukikimoto profile image
Yuki Kimoto • Edited

What is the env key?

Is it means the following?

${{ env.PERL5LIB }}
Enter fullscreen mode Exit fullscreen mode

Or

env:
Enter fullscreen mode Exit fullscreen mode
Collapse
 
szabgab profile image
Gabor Szabo

the latter

Thread Thread
 
yukikimoto profile image
Yuki Kimoto

What is the env key?

The reason is PERL5LIB environment variable must be set dynamically using $Config{archname} in steps.

In this case, it looks like the syntax of bash or pwsh must be used.


About environment variables

docs.github.com/en/actions/learn-g...

Because environment variable interpolation is done after a workflow job is sent to a runner machine, you must use the appropriate syntax for the shell that's used on the runner. In this example, the workflow specifies ubuntu-latest. By default, Linux runners use the bash shell, so you must use the syntax $NAME. If the workflow specified a Windows runner, you would use the syntax for PowerShell, $env:NAME. For more information about shells, see "Workflow syntax for GitHub Actions."