1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

plenv で virtualenv な話

Posted at

仕事に詰まって現実逃避。
元ネタはここ
perldoc が付いてなかったのが不満だったのと、単独での実行はいらない事と、親パスまでは決め打ちで良い事から、改造。

plenv 入れてあるので、その perl にて。

  • plenv 以外の使用を前提としない。
  • 元の perl は、cpanm, local::lib が入っていること
  • 箱作る所だけ、下のスクリプト。
  • 使用は、plenv まかせ。
    • plenv shell とか、plenv uninstall とか、
  • 外部コマンドのソースからのインストール時には、このスクリプトで作成した perl 以外の perl でインストールを。
    • pod2man あたりでトラブルの危険
    • Makefile とか直接弄るんでもいいけど

実行

$ plenv versions
  system
* 5.20.1 (set by PLENV_VERSION environment variable)
$ perl /PATH/TO/venv.pl oppai
$ plenv versions
  system
* 5.20.1 (set by PLENV_VERSION environment variable)
  oppai
$ plenv shell oppai
$ cpanm Acme::Oppai
$ plenv rehash
$ perldoc Acme::Oppai
# (略)
$ perl -MAcme::Oppai -le 'print Acme::Oppai->new->massage;'
    _  ∩
  ( ゜∀゜)彡 おっぱい!おっぱい!
  (  ⊂彡
   |   | 
   し ⌒J

$ plenv uninstall oppai

ソース

venv.pl
# !/usr/bin/env perl
use strict;
use warnings;

use Config;
use File::Path qw(make_path);

die if ! defined $ENV{PLENV_ROOT} ;
die if ! defined $ARGV[0] ;
my $new_path    = join q{/}, $ENV{PLENV_ROOT}, q{versions}, $ARGV[0] ;
my $new_bin     = join q{/}, $new_path, q{bin} ;
my $new_perl    = join q{/}, $new_bin,  q{perl} ;
my $new_doc     = join q{/}, $new_bin,  q{perldoc} ;
my $new_cpanm   = join q{/}, $new_bin,  q{cpanm} ; 
my $new_lib_dir = join q{/}, $new_path, q{lib}, q{perl5} ;


my $orig_bin    = $Config{bin} ;
my $orig_perl   = $Config{perlpath} ;
my $orig_doc    = join q{/}, $orig_bin, q{perldoc} ;
my $orig_cpanm  = join q{/}, $orig_bin, q{cpanm} ;

die if -T $orig_perl ;
die if -l $orig_perl ;
die if ! -f $orig_doc ;
die if ! -f $orig_cpanm ;

die if qx{$orig_doc -l local::lib} !~ m{^/.*?/lib\.pm$} ;

make_path $new_bin if ! -d $new_bin;
{
    
    open my $fh, q{>}, $new_perl ;
    print {$fh} <<"EOF" ;
# !/bin/sh
exec $orig_perl -Mlocal::lib=$new_path "\$\@"
EOF

    close $fh ;
    chmod 0755, $new_perl ;
}

{
    open my $fh, q{>}, $new_cpanm ;
    print {$fh} <<"EOF" ;
# !/bin/sh
exec $orig_cpanm --local-lib=$new_path "\$\@"
EOF

    close $fh ;
    chmod 0755, $new_cpanm ;
}

{
    open my $fh, q{>}, $new_doc ;
    print {$fh} <<"EOF" ;
# !/bin/sh
export PERL5LIB=$new_lib_dir
exec $orig_doc "\$\@"
EOF

    close $fh ;
    chmod 0755, $new_doc ;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?