Perl Virtual Environments

Posted by Joe Topjian on July 28, 2010 under Development | 5 Comments to Read

Introduction

One nice tool for Python is virtualenv. It allows you to create sandboxed Python library repositories and enable them and disable them at will. By utilizing this, you are able to work on separate projects that can each have their own individual environment and not affect other environments.

Perl has something similar called local::lib. I recently started using it, but I missed the virtualenv wrapper script that comes with Python’s virtualenv, so I decided to write my own for Perl.

Table of Contents

local::lib Installation

As with any Perl module, local::lib can be installed in a myriad of different ways. I won’t detail every single method here. If you want to get up and running with local::lib, just do:

$ cpanp "i local::lib"

penv.pl Script

Once local::lib is installed, download my penv.pl script. This script is based largely off of virtualenv‘s wrapper script and basically does the same thing.

To create a new Perl environment, do:

$ penv.pl ptest

This will create a new directory called ptest. You can then activate all required environment variables by doing:

$ source ptest/bin/activate

You’ll notice your prompt will change to denote the new environment.

You may now install any Perl module you’d like and it will be installed locally to ptest.

To deactivate the environment, just do:

$ deactivate

Conclusion

Creating virtual environments are a very handy feature. They ensure your desktop or server does not get muddied up with hundreds of libraries that you either don’t use any more or could cause potential conflicts.

The penv.pl script is a nice wrapper script to local::lib. Please let me know if you find it useful or run into any problems with it.

  • Perl Development » Terrarum said,

    [...] begin developing — or even just toying around — I create a new Perl Virtual Environment. Once the environment is set up, I have a few standard modules I install (note: some of these [...]

  • trwww said,

    Pretty slick!

    I just compile perl from source and use the path setting to configure:

    sh Configure -Dprefix=/home/me/projA/perl -de

    and then update the current terminal’s PATH:

    export PATH=”/home/me/projA/perl/bin:$PATH”

    then not only can I swap out different libraries, but different versions of perl itself.

    Before I do much fiddling to the install, I archive the perl directory or the app directory:

    tar -zcf projA.tgz projA

    then I can do whatever I want in the app testing out different stuff. Sometimes I keep the whole project in source control and put different versions of stuff in branches.

    But your approach makes that particular procedure very quick to swap stuff out. Thanks!

  • po said,

    trwww: You might take a look at App::perlbrew http://search.cpan.org/dist/App-perlbrew/ It’s a variation on exactly what you have described.

  • draegtun said,

    Checkout plenv

    http://github.com/tokuhirom/plenv

  • Python Virtualenv – A Step Toward Sanity « ionull.com said,

    [...] virtualenv instructions Official virtualenv docs Perl virtualenv module Posted in [...]

Add A Comment