@@ -10,27 +10,29 @@ $fatpacked{"App/perlbrew.pm"} = <<'APP_PERLBREW';
10
10
use warnings;
11
11
use 5.008;
12
12
use Getopt::Long ();
13
- use File::Spec::Functions qw( catfile );
13
+ use File::Spec::Functions qw( catfile catdir );
14
14
use File::Path::Tiny;
15
15
use Text::Levenshtein ();
16
16
use FindBin;
17
17
18
18
our $VERSION = "0.29";
19
19
our $CONF;
20
20
21
- our $PERLBREW_ROOT || = $ENV{PERLBREW_ROOT} || "$ENV{HOME}/perl5/perlbrew";
22
- our $PERLBREW_HOME || = $ENV{PERLBREW_HOME} || "$ENV{HOME}/.perlbrew";
21
+ our $PERLBREW_ROOT = $ENV{PERLBREW_ROOT} || "$ENV{HOME}/perl5/perlbrew";
22
+ our $PERLBREW_HOME = $ENV{PERLBREW_HOME} || "$ENV{HOME}/.perlbrew";
23
23
24
24
my $CONF_FILE = catfile( $PERLBREW_ROOT, 'Conf.pm' );
25
- my $CURRENT_PERL = $ENV{PERLBREW_PERL};
26
25
27
26
local $SIG{__DIE__} = sub {
28
27
my $message = shift;
29
28
warn $message;
30
29
exit 1;
31
30
};
32
31
33
- sub current_perl { $CURRENT_PERL || '' }
32
+ sub current_perl {
33
+ my ($self) = @_;
34
+ return $self->env('PERLBREW_PERL') || ''
35
+ }
34
36
35
37
sub BASHRC_CONTENT() {
36
38
return "export PERLBREW_BASHRC_VERSION=$VERSION\n\n" . <<'RC';
@@ -83,19 +85,18 @@ $fatpacked{"App/perlbrew.pm"} = <<'APP_PERLBREW';
83
85
(use)
84
86
if [[ -z "$2" ]] ; then
85
87
if [[ -z "$PERLBREW_PERL" ]] ; then
86
- echo "No version in use; defaulting to system"
88
+ echo "Currently using system perl "
87
89
else
88
- echo "Using $PERLBREW_PERL version "
90
+ echo "Currently using $PERLBREW_PERL"
89
91
fi
90
- elif [[ -x "$PERLBREW_ROOT/perls/$2/bin/perl" ]]; then
91
- eval $(command perlbrew $short_option env $2)
92
- __perlbrew_set_path
93
- elif [[ -x "$PERLBREW_ROOT/perls/perl-$2/bin/perl" ]]; then
94
- eval $(command perlbrew $short_option env "perl-$2")
95
- __perlbrew_set_path
96
92
else
97
- echo "$2 is not installed" >&2
98
- exit_status=1
93
+ code=$(command perlbrew env $2);
94
+ if [ -z "$code" ]; then
95
+ exit_status=1
96
+ else
97
+ eval $code
98
+ __perlbrew_set_path
99
+ fi
99
100
fi
100
101
;;
101
102
@@ -913,7 +914,7 @@ $fatpacked{"App/perlbrew.pm"} = <<'APP_PERLBREW';
913
914
push @result, {
914
915
name => $name,
915
916
version => $self->format_perl_version(`$executable -e 'print \$]'`),
916
- is_current => (current_perl eq $name),
917
+ is_current => ($self-> current_perl eq $name),
917
918
is_external => 0
918
919
};
919
920
}
@@ -944,23 +945,39 @@ $fatpacked{"App/perlbrew.pm"} = <<'APP_PERLBREW';
944
945
945
946
# Return a hash of PERLBREW_* variables
946
947
sub perlbrew_env {
947
- my ($self, $perl ) = @_;
948
+ my ($self, $name ) = @_;
948
949
949
950
my %env = (
950
951
PERLBREW_VERSION => $VERSION,
951
952
PERLBREW_PATH => "$PERLBREW_ROOT/bin",
952
953
PERLBREW_ROOT => $PERLBREW_ROOT
953
954
);
954
955
955
- if ($perl) {
956
- if(-d "$PERLBREW_ROOT/perls/$perl/bin") {
957
- $env{PERLBREW_PERL} = $perl;
958
- $env{PERLBREW_PATH} .= ":$PERLBREW_ROOT/perls/$perl/bin";
956
+ if ($name) {
957
+ my ($perl_name, $lib_name) = split("@", $name);
958
+ $perl_name = $name unless $lib_name;
959
+
960
+ if(-d "$PERLBREW_ROOT/perls/$perl_name/bin") {
961
+ $env{PERLBREW_PERL} = $perl_name;
962
+ $env{PERLBREW_PATH} .= ":$PERLBREW_ROOT/perls/$perl_name/bin";
963
+ }
964
+
965
+ if ($lib_name) {
966
+ require local::lib;
967
+ my $base = "$PERLBREW_HOME/libs/$name";
968
+
969
+ my %lib_env = local::lib->build_environment_vars_for($base, 0, 0);
970
+
971
+ $env{PERLBREW_PATH} = "$base/bin:" . $env{PERLBREW_PATH};
972
+ $env{PERLBREW_LIB} = $lib_name;
973
+ $env{PERL_MM_OPT} = $lib_env{PERL_MM_OPT};
974
+ $env{PERL_MB_OPT} = $lib_env{PERL_MB_OPT};
975
+ $env{PERL5LIB} = $lib_env{PERL5LIB};
976
+ $env{PERL_LOCAL_LIB_ROOT} = $lib_env{PERL_LOCAL_LIB_ROOT};
959
977
}
960
978
}
961
- elsif ( $self->env("PERLBREW_PERL") ) {
962
- $env{PERLBREW_PERL} = $self->env("PERLBREW_PERL");
963
- $env{PERLBREW_PATH} .= ":$PERLBREW_ROOT/perls/$env{PERLBREW_PERL}/bin";
979
+ else {
980
+ $env{PERLBREW_PERL} = "";
964
981
}
965
982
966
983
return %env;
@@ -1136,12 +1153,14 @@ $fatpacked{"App/perlbrew.pm"} = <<'APP_PERLBREW';
1136
1153
1137
1154
if ($self->env('SHELL') =~ /(ba|k|z|\/)sh$/) {
1138
1155
while (my ($k, $v) = each(%env)) {
1139
- print "export $k=$v\n";
1156
+ $v =~ s/(\\")/\\$1/g if defined $v;
1157
+ print "export $k=\"$v\"\n";
1140
1158
}
1141
1159
}
1142
1160
else {
1143
1161
while (my ($k, $v) = each(%env)) {
1144
- print "setenv $k $v\n";
1162
+ $v =~ s/(\\")/\\$1/g if defined $v;
1163
+ print "setenv $k \"$v\"\n";
1145
1164
}
1146
1165
}
1147
1166
}
@@ -1183,6 +1202,8 @@ $fatpacked{"App/perlbrew.pm"} = <<'APP_PERLBREW';
1183
1202
die "\nERROR: Failed to retrive cpanm executable.\n\n";
1184
1203
}
1185
1204
1205
+ mkpath("$PERLBREW_ROOT/bin") unless -d "$PERLBREW_ROOT/bin";
1206
+
1186
1207
open my $CPANM, '>', $out or die "cannot open file($out): $!";
1187
1208
print $CPANM $body;
1188
1209
close $CPANM;
@@ -1379,15 +1400,94 @@ $fatpacked{"App/perlbrew.pm"} = <<'APP_PERLBREW';
1379
1400
print CSHRC_CONTENT;
1380
1401
}
1381
1402
1403
+ sub run_command_lib {
1404
+ my ($self, $subcommand, @args) = @_;
1405
+ unless ($subcommand) {
1406
+ print <<'USAGE';
1407
+
1408
+ The 'lib' command can be used to manage multiple local::lib containers
1409
+ inside different perls. Here are some a brief usage.
1410
+
1411
+ # Assuming perl-5.14.2 for the following examples.
1412
+ perlbrew switch 5.14.2
1413
+
1414
+ # Create a local::lib folder named `nobita` inside current perl
1415
+ perlbrew lib create nobita
1416
+
1417
+ # Create multiple local::lib folders in one command.
1418
+ perlbrew lib create nobita shizuka naruto
1419
+
1420
+ # Get a list of local::lib folders
1421
+ perlbrew lib list
1422
+
1423
+ # Create a local::lib folder named `shizuka` inside perl-5.12.3,
1424
+ # ... without activating to perl-5.12.3 first.
1425
+ perlbrew lib create perl-5.12.3@shizuka
1426
+
1427
+ # Activate perl-5.12.3, with the 'nobita' local::lib
1428
+ perlbrew use perl-5.12.3@nobita
1429
+
1430
+ # Activate perl-5.14.2, with the 'nobita' local::lib
1431
+ perlbrew use perl-5.14.2@nobita
1432
+
1433
+ # Make perl-5.14.2@nobita the default perl + local::lib setting for new shells.
1434
+ perlbrew switch perl-5.14.2@nobita
1435
+
1436
+ # Remove libs, notice `shizuka` here means `perl-5.14.2@shizuka`
1437
+ perlbrew lib remove perl-5.12.3@nobita shizuka
1438
+
1439
+ # Back to a local::lib-less state.
1440
+ perlbrew switch perl-5.14.2
1441
+
1442
+ USAGE
1443
+ return;
1444
+ }
1445
+
1446
+ my $sub = "run_command_lib_$subcommand";
1447
+ if ($self->can($sub)) {
1448
+ $self->$sub( @args );
1449
+ }
1450
+ else {
1451
+ print "Unknown command: $subcommand\n";
1452
+ }
1453
+ }
1454
+
1455
+ sub run_command_lib_create {
1456
+ my ($self, $name) = @_;
1457
+
1458
+ my $fullname = $self->current_perl . '@' . $name;
1459
+ mkpath( catdir($PERLBREW_HOME, "libs", $fullname) );
1460
+
1461
+ print "lib '$fullname' is created.\n"
1462
+ unless $self->{quiet};
1463
+
1464
+ return;
1465
+ }
1466
+
1467
+ sub run_command_lib_delete {
1468
+ my ($self, $name) = @_;
1469
+
1470
+ my $fullname = $self->current_perl . '@' . $name;
1471
+ rmpath( catdir($PERLBREW_HOME, "libs", $fullname) );
1472
+
1473
+ print "lib '$fullname' is deleted.\n"
1474
+ unless $self->{quiet};
1475
+
1476
+ return;
1477
+ }
1478
+
1382
1479
sub resolve_installation_name {
1383
1480
my ($self, $name) = @_;
1384
1481
die "App::perlbrew->resolve_installation_name requires one argument." unless $name;
1385
1482
1386
- if ( $self->is_installed($name) ) {
1483
+ my ($perl_name, $lib_name) = split('@', $name);
1484
+ $perl_name = $name unless $lib_name;
1485
+
1486
+ if ( $self->is_installed($perl_name) ) {
1387
1487
return $name;
1388
1488
}
1389
- elsif ($self->is_installed("perl-$name ")) {
1390
- return "perl-$name";
1489
+ elsif ($self->is_installed("perl-${perl_name} ")) {
1490
+ return "perl-${ name} ";
1391
1491
}
1392
1492
1393
1493
return undef;
0 commit comments