https://github.com/kiwiroy/perlbrewr
run perl scripts in Rmarkdown with perlbrew environment
https://github.com/kiwiroy/perlbrewr
knitr perl perl5 perlbrew r reproducibility rmarkdown
Last synced: about 2 months ago
JSON representation
run perl scripts in Rmarkdown with perlbrew environment
- Host: GitHub
- URL: https://github.com/kiwiroy/perlbrewr
- Owner: kiwiroy
- License: other
- Created: 2019-02-20T10:16:44.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-04-27T23:09:53.000Z (about 6 years ago)
- Last Synced: 2025-05-15T17:14:48.137Z (about 1 year ago)
- Topics: knitr, perl, perl5, perlbrew, r, reproducibility, rmarkdown
- Language: R
- Homepage: https://kiwiroy.github.io/perlbrewr/
- Size: 186 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
params:
root: !r Sys.getenv("PERLBREW_ROOT")
perl_version: 5.24.3
---
[](https://travis-ci.org/kiwiroy/perlbrewr)
[](https://coveralls.io/r/kiwiroy/perlbrewr?branch=master)
[](https://www.tidyverse.org/lifecycle/#experimental)
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
Sys.setenv("PERLBREW_ROOT"=params$root)
tmp <- file.path(tempdir(), ".perlbrew")
if(!dir.exists(tmp)) {
dir.create(tmp)
}
Sys.setenv("PERLBREW_HOME"=tmp)
Sys.setenv("perlbrew_command"=file.path(params$root, "bin", "perlbrew"))
```
# perlbrewr
The goal of perlbrewr is to assist the loading of a [perlbrew](https://perlbrew.pl)
perl and optionally a library with the aim of improving reproducibility. The
central task that perlbrewr performs is management of the environment variables
in the same manner as perlbrew itself, by calling perlbrew commands and
translating the changes there into R function calls that achieve the same
outcome. Primarily, these are `Sys.setenv` and `Sys.unsetenv`.
## Dependencies
### R
* R (>= 3.3.0)
* magrittr
* stringr
### Non R
* [perlbrew](https://perlbrew.pl)
## Installation
You can install the released version of perlbrewr from [GitHub](https://github.com/kiwiroy/perlbrewr) with:
``` r
devtools::install_github("kiwiroy/perlbrewr")
```
## Example
This is a basic example of usage to load a perlbrew environment:
`params$perl_version` = ``r params$perl_version``
```{r example}
library(perlbrewr)
result <- perlbrew(root = Sys.getenv("PERLBREW_ROOT"), version = params$perl_version)
```
The brewed version of perl is now the default.
```{r perlversion}
Sys.which("perl")
```
This is also the case in `bash` shell blocks.
```{r perlversion-in-bash, engine = "bash"}
which perl
```
By configuring `knitr` - this happens automatically by default.
```{r knitr-config}
knitr::opts_chunk$set(engine.path = list(perl = Sys.which("perl")[["perl"]]))
```
Perl code in `perl` blocks run the same interpreter.
```{r perl-code, engine = "perl"}
print "$^X\n";
```
### local::lib library access
Perlbrew supports [`local::lib`](https://metacpan.org/pod/local::lib) libraries for further controlling which modules are installed. `perlbrewr` supports loading these also.
```{r create-lib, include=FALSE}
perlbrew_lib_create(version = params$perl_version, lib = "example")
```
```{r use-lib}
perlbrew(version = params$perl_version, lib = "example")
Sys.getenv("PERL5LIB")
```
Within this `local::lib` modules may be installed with [`cpanm`](https://metacpan.org/pod/App::cpanminus).
```{r install-something, engine = "bash"}
cd inst
cpanm -n -q --installdeps .
```
Since `perlbrewr::perlbrew` sets the `PERL5LIB` environment variable perl code
relying on the dependencies is now sucessful.
```{r perl-code-2, engine = "perl"}
use Mojo::Base -strict;
use Mojo::File;
say Mojo::File->new('inst/cpanfile')->slurp;
```
### listing and creating libraries
```{r re-root, include = FALSE}
brew_list <- perlbrew_list()
if(length(brew_list) > 12) {
## This shortens the lists in the next two
root <- file.path(getwd(),"tests","testthat","mock")
pbcmd <- file.path(root, "bin", "perlbrew")
Sys.setenv("PERLBREW_ROOT"=root, "perlbrew_command"=pbcmd)
}
```
`perlbrew_list` returns a listing of the available versions of perl and any `local::lib` libraries. If a version or library is in use, the `active` object attribute is also set.
```{r perlbrew-list}
perlbrew_list()
```
A new library is created with `perlbrew_lib_create`.
```{r perlbrew-lib-create-show}
perlbrew_list()
```
### knitr
The knitr chunk options `engine.path` and `engine.opts` are set automatically so that each `engine="perl"` chunk will use the correct `perl` interpreter and `PERL5LIB`. Any `engine.opts` for perl that have already been set should remain in the list. For this to work correctly the `list()` version of the `engine.opts` should be used. i.e.
```r
knitr::opts_chunk$set(engine.opts = list(perl = "-CS", bash = "--norc"))
```