-
Notifications
You must be signed in to change notification settings - Fork 216
/
perlbrew
executable file
·11102 lines (8504 loc) · 346 KB
/
perlbrew
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
use strict;
use Config;
BEGIN {
my @oldinc = @INC;
@INC = ( $Config{sitelibexp}."/".$Config{archname}, $Config{sitelibexp}, @Config{qw<vendorlibexp vendorarchexp archlibexp privlibexp>} );
require Cwd;
@INC = @oldinc;
}
# This chunk of stuff was generated by App::FatPacker. To find the original
# file's code, look for the end of this BEGIN block or the string 'FATPACK'
BEGIN {
my %fatpacked;
$fatpacked{"App/Perlbrew/HTTP.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_PERLBREW_HTTP';
package App::Perlbrew::HTTP;
use strict;
use warnings;
use 5.008;
use Exporter 'import';
our @EXPORT_OK = qw(http_user_agent_program http_user_agent_command http_get http_download);
our $HTTP_VERBOSE = 0;
our $HTTP_USER_AGENT_PROGRAM;
my %commands = (
curl => {
test => '--version >/dev/null 2>&1',
get => '--silent --location --fail -o - {url}',
download => '--silent --location --fail -o {output} {url}',
order => 1,
# Exit code is 22 on 404s etc
die_on_error => sub { die 'Page not retrieved; HTTP error code 400 or above.' if ($_[ 0 ] >> 8 == 22); },
},
wget => {
test => '--version >/dev/null 2>&1',
get => '--quiet -O - {url}',
download => '--quiet -O {output} {url}',
order => 2,
# Exit code is not 0 on error
die_on_error => sub { die 'Page not retrieved: fetch failed.' if ($_[ 0 ]); },
},
fetch => {
test => '--version >/dev/null 2>&1',
get => '-o - {url}',
download => '-o {output} {url}',
order => 3,
# Exit code is 8 on 404s etc
die_on_error => sub { die 'Server issued an error response.' if ($_[ 0 ] >> 8 == 8); },
}
);
sub http_user_agent_program {
$HTTP_USER_AGENT_PROGRAM ||= do {
my $program;
for my $p (sort {$commands{$a}{order}<=>$commands{$b}{order}} keys %commands) {
my $code = system("$p $commands{$p}->{test}") >> 8;
if ($code != 127) {
$program = $p;
last;
}
}
unless ($program) {
die "[ERROR] Cannot find a proper http user agent program. Please install curl or wget.\n";
}
$program;
};
die "[ERROR] Unrecognized http user agent program: $HTTP_USER_AGENT_PROGRAM. It can only be one of: ".join(",", keys %commands)."\n" unless $commands{$HTTP_USER_AGENT_PROGRAM};
return $HTTP_USER_AGENT_PROGRAM;
}
sub http_user_agent_command {
my ($purpose, $params) = @_;
my $ua = http_user_agent_program;
my $cmd = $commands{ $ua }->{ $purpose };
for (keys %$params) {
$cmd =~ s!{$_}!\Q$params->{$_}\E!g;
}
if ($HTTP_VERBOSE) {
unless ($ua eq "fetch") {
$cmd =~ s/(silent|quiet)/verbose/;
}
}
$cmd = $ua . " " . $cmd;
return ($ua, $cmd) if wantarray;
return $cmd;
}
sub http_download {
my ($url, $path) = @_;
if (-e $path) {
die "ERROR: The download target < $path > already exists.\n";
}
my $partial = 0;
local $SIG{TERM} = local $SIG{INT} = sub { $partial++ };
my $download_command = http_user_agent_command(download => { url => $url, output => $path });
my $status = system($download_command);
if ($partial) {
$path->unlink;
return "ERROR: Interrupted.";
}
unless ($status == 0) {
$path->unlink;
if ($? == -1) {
return "ERROR: Failed to execute the command\n\n\t$download_command\n\nReason:\n\n\t$!";
}
elsif ($? & 127) {
return "ERROR: The command died with signal " . ($? & 127) . "\n\n\t$download_command\n\n";
}
else {
return "ERROR: The command finished with error\n\n\t$download_command\n\nExit code:\n\n\t" . ($? >> 8);
}
}
return 0;
}
sub http_get {
my ($url, $header, $cb) = @_;
if (ref($header) eq 'CODE') {
$cb = $header;
$header = undef;
}
my ($program, $command) = http_user_agent_command(get => { url => $url });
open my $fh, '-|', $command
or die "open() pipe for '$command': $!";
local $/;
my $body = <$fh>;
close $fh;
# check if the download has failed and die automatically
$commands{ $program }{ die_on_error }->($?);
return $cb ? $cb->($body) : $body;
}
1;
APP_PERLBREW_HTTP
$fatpacked{"App/Perlbrew/Path.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_PERLBREW_PATH';
use strict;
use warnings;
package App::Perlbrew::Path;
use File::Basename ();
use File::Glob ();
use File::Path ();
use overload (
'""' => \& stringify,
fallback => 1,
);
sub _joinpath {
for my $entry (@_) {
no warnings 'uninitialized';
die 'Received an undefined entry as a parameter (all parameters are: '. join(', ', @_). ')' unless (defined($entry));
}
return join "/", @_;
}
sub _child {
my ($self, $package, @path) = @_;
$package->new($self->{path}, @path);
}
sub _children {
my ($self, $package) = @_;
map { $package->new($_) } File::Glob::bsd_glob($self->child("*"));
}
sub new {
my ($class, @path) = @_;
bless { path => _joinpath (@path) }, $class;
}
sub exists {
my ($self) = @_;
-e $self->stringify;
}
sub basename {
my ($self, $suffix) = @_;
return scalar File::Basename::fileparse($self, ($suffix) x!! defined $suffix);
}
sub child {
my ($self, @path) = @_;
return $self->_child(__PACKAGE__, @path);
}
sub children {
my ($self) = @_;
return $self->_children(__PACKAGE__);
}
sub dirname {
my ($self) = @_;
return App::Perlbrew::Path->new( File::Basename::dirname($self) );
}
sub mkpath {
my ($self) = @_;
File::Path::mkpath( [$self->stringify], 0, 0777 );
return $self;
}
sub readlink {
my ($self) = @_;
my $link = CORE::readlink( $self->stringify );
$link = __PACKAGE__->new($link) if defined $link;
return $link;
}
sub rmpath {
my ($self) = @_;
File::Path::rmtree( [$self->stringify], 0, 0 );
return $self;
}
sub stringify {
my ($self) = @_;
return $self->{path};
}
sub stringify_with_tilde {
my ($self) = @_;
my $path = $self->stringify;
my $home = $ENV{HOME};
$path =~ s!\Q$home/\E!~/! if $home;
return $path;
}
sub symlink {
my ($self, $destination, $force) = @_;
$destination = App::Perlbrew::Path->new($destination) unless ref $destination;
CORE::unlink($destination) if $force && (-e $destination || -l $destination);
$destination if CORE::symlink($self, $destination);
}
sub unlink {
my ($self) = @_;
CORE::unlink($self);
}
1;
APP_PERLBREW_PATH
$fatpacked{"App/Perlbrew/Path/Installation.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_PERLBREW_PATH_INSTALLATION';
use strict;
use warnings;
package App::Perlbrew::Path::Installation;
require App::Perlbrew::Path;
our @ISA = qw( App::Perlbrew::Path );
sub name {
$_[0]->basename;
}
sub bin {
shift->child(bin => @_);
}
sub man {
shift->child(man => @_);
}
sub perl {
shift->bin('perl');
}
sub version_file {
shift->child('.version');
}
1;
APP_PERLBREW_PATH_INSTALLATION
$fatpacked{"App/Perlbrew/Path/Installations.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_PERLBREW_PATH_INSTALLATIONS';
use strict;
use warnings;
package App::Perlbrew::Path::Installations;
require App::Perlbrew::Path;
require App::Perlbrew::Path::Installation;
our @ISA = qw( App::Perlbrew::Path );
sub child {
my ($self, @params) = @_;
my $return = $self;
$return = $return->_child('App::Perlbrew::Path::Installation' => shift @params) if @params;
$return = $return->child(@params) if @params;
$return;
}
sub children {
shift->_children('App::Perlbrew::Path::Installation' => @_);
}
sub list {
shift->children;
}
1;
APP_PERLBREW_PATH_INSTALLATIONS
$fatpacked{"App/Perlbrew/Path/Root.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_PERLBREW_PATH_ROOT';
use strict;
use warnings;
package App::Perlbrew::Path::Root;
use App::Perlbrew::Path ();
use App::Perlbrew::Path::Installations ();
our @ISA = qw( App::Perlbrew::Path );
sub bin {
shift->child(bin => @_);
}
sub build {
shift->child(build => @_);
}
sub dists {
shift->child(dists => @_);
}
sub etc {
shift->child(etc => @_);
}
sub perls {
my ($self, @params) = @_;
my $return = $self->_child('App::Perlbrew::Path::Installations', 'perls');
$return = $return->child(@params) if @params;
return $return;
}
1;
APP_PERLBREW_PATH_ROOT
$fatpacked{"App/Perlbrew/Sys.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_PERLBREW_SYS';
package App::Perlbrew::Sys;
use strict;
use warnings;
use Config;
sub osname {
$Config{osname}
}
sub archname {
$Config{archname}
}
sub os {
$Config{osname}
}
sub arch {
(split(/-/, $Config{myarchname}, 2))[0]
}
1;
APP_PERLBREW_SYS
$fatpacked{"App/Perlbrew/Util.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_PERLBREW_UTIL';
package App::Perlbrew::Util;
use strict;
use warnings;
use 5.008;
use Exporter 'import';
our @EXPORT = qw( uniq min editdist files_are_the_same perl_version_to_integer );
our @EXPORT_OK = qw(
find_similar_tokens
looks_like_url_of_skaji_relocatable_perl
looks_like_sys_would_be_compatible_with_skaji_relocatable_perl
make_skaji_relocatable_perl_url
);
sub uniq {
my %seen;
grep { !$seen{$_}++ } @_;
}
sub min(@) {
my $m = $_[0];
for(@_) {
$m = $_ if $_ < $m;
}
return $m;
}
# straight copy of Wikipedia's "Levenshtein Distance"
sub editdist {
my @a = split //, shift;
my @b = split //, shift;
# There is an extra row and column in the matrix. This is the
# distance from the empty string to a substring of the target.
my @d;
$d[$_][0] = $_ for (0 .. @a);
$d[0][$_] = $_ for (0 .. @b);
for my $i (1 .. @a) {
for my $j (1 .. @b) {
$d[$i][$j] = ($a[$i-1] eq $b[$j-1] ? $d[$i-1][$j-1]
: 1 + min($d[$i-1][$j], $d[$i][$j-1], $d[$i-1][$j-1]));
}
}
return $d[@a][@b];
}
sub files_are_the_same {
## Check dev and inode num. Not useful on Win32.
## The for loop should always return false on Win32, as a result.
my @files = @_;
my @stats = map {[ stat($_) ]} @files;
my $stats0 = join " ", @{$stats[0]}[0,1];
for (@stats) {
return 0 if ((! defined($_->[1])) || $_->[1] == 0);
unless ($stats0 eq join(" ", $_->[0], $_->[1])) {
return 0;
}
}
return 1
}
sub perl_version_to_integer {
my $version = shift;
my @v;
if ($version eq 'blead') {
@v = (999,999,999);
} else {
@v = split(/[\.\-_]/, $version);
}
return undef if @v < 2;
if ($v[1] <= 5) {
$v[2] ||= 0;
$v[3] = 0;
}
else {
$v[3] ||= $v[1] >= 6 ? 9 : 0;
$v[3] =~ s/[^0-9]//g;
}
return $v[1]*1000000 + $v[2]*1000 + $v[3];
}
sub find_similar_tokens {
my ($token, $tokens) = @_;
my $SIMILAR_DISTANCE = 6;
my @similar_tokens = sort { $a->[1] <=> $b->[1] } map {
my $d = editdist( $_, $token );
( ( $d < $SIMILAR_DISTANCE ) ? [$_, $d] : () )
} @$tokens;
if (@similar_tokens) {
my $best_score = $similar_tokens[0][1];
@similar_tokens = map { $_->[0] } grep { $_->[1] == $best_score } @similar_tokens;
}
return \@similar_tokens;
}
sub looks_like_url_of_skaji_relocatable_perl {
my ($str) = @_;
# https://github.com/skaji/relocatable-perl/releases/download/5.40.0.0/perl-linux-amd64.tar.gz
my $prefix = "https://github.com/skaji/relocatable-perl/releases/download";
my $version_re = qr/(5\.[0-9][0-9]\.[0-9][0-9]?.[0-9])/;
my $name_re = qr/perl-(linux|darwin)-(amd64|arm64)\.tar\.gz/;
return undef unless $str =~ m{ \Q$prefix\E / $version_re / $name_re }x;
return {
url => $str,
version => $1,
os => $2,
arch => $3,
original_filename => "perl-$2-$3.tar.gz",
};
}
sub _arch_compat {
my ($arch) = @_;
my $compat = {
x86_64 => "amd64",
i386 => "amd64",
};
return $compat->{$arch} || $arch;
}
sub looks_like_sys_would_be_compatible_with_skaji_relocatable_perl {
my ($detail, $sys) = @_;
return (
($detail->{os} eq $sys->os)
&& (_arch_compat($detail->{arch}) eq _arch_compat($sys->arch))
);
}
sub make_skaji_relocatable_perl_url {
my ($str, $sys) = @_;
if ($str =~ m/\Askaji-relocatable-perl-(5\.[0-9][0-9]\.[0-9][0-9]?.[0-9])\z/) {
my $version = $1;
my $os = $sys->os;
my $arch = $sys->arch;
$arch = "amd64" if $arch eq 'x86_64' || $arch eq 'i386';
return "https://github.com/skaji/relocatable-perl/releases/download/$version/perl-$os-$arch.tar.gz";
}
return undef;
}
1;
APP_PERLBREW_UTIL
$fatpacked{"App/perlbrew.pm"} = '#line '.(1+__LINE__).' "'.__FILE__."\"\n".<<'APP_PERLBREW';
package App::perlbrew;
use strict;
use warnings;
use 5.008;
our $VERSION = "1.00";
use Config qw( %Config );
BEGIN {
# Special treat for Cwd to prevent it to be loaded from somewhere binary-incompatible with system perl.
my @oldinc = @INC;
@INC = (
$Config{sitelibexp} . "/" . $Config{archname},
$Config{sitelibexp}, @Config{qw<vendorlibexp vendorarchexp archlibexp privlibexp>},
);
require Cwd;
@INC = @oldinc;
}
use Getopt::Long ();
use CPAN::Perl::Releases ();
use JSON::PP qw( decode_json );
use File::Copy qw( copy move );
use Capture::Tiny ();
use App::Perlbrew::Util qw( files_are_the_same uniq find_similar_tokens looks_like_url_of_skaji_relocatable_perl looks_like_sys_would_be_compatible_with_skaji_relocatable_perl make_skaji_relocatable_perl_url );
use App::Perlbrew::Path ();
use App::Perlbrew::Path::Root ();
use App::Perlbrew::HTTP qw( http_download http_get );
use App::Perlbrew::Sys;
### global variables
# set $ENV{SHELL} to executable path of parent process (= shell) if it's missing
# (e.g. if this script was executed by a daemon started with "service xxx start")
# ref: https://github.com/gugod/App-perlbrew/pull/404
$ENV{SHELL} ||= App::Perlbrew::Path->new( "/proc", getppid, "exe" )->readlink if -d "/proc";
local $SIG{__DIE__} = sub {
my $message = shift;
warn $message;
exit(1);
};
our $CONFIG;
our $PERLBREW_ROOT;
our $PERLBREW_HOME;
my @flavors = (
{
d_option => 'usethreads',
implies => 'multi',
common => 1,
opt => 'thread|threads'
}, # threads is for backward compatibility
{
d_option => 'usemultiplicity',
opt => 'multi'
},
{
d_option => 'uselongdouble',
common => 1,
opt => 'ld'
},
{
d_option => 'use64bitint',
common => 1,
opt => '64int'
},
{
d_option => 'use64bitall',
implies => '64int',
opt => '64all'
},
{
d_option => 'DEBUGGING',
opt => 'debug'
},
{
d_option => 'cc=clang',
opt => 'clang'
},
);
my %flavor;
my $flavor_ix = 0;
for (@flavors) {
my ($name) = $_->{opt} =~ /([^|]+)/;
$_->{name} = $name;
$_->{ix} = ++$flavor_ix;
$flavor{$name} = $_;
}
for (@flavors) {
if ( my $implies = $_->{implies} ) {
$flavor{$implies}{implied_by} = $_->{name};
}
}
### methods
sub new {
my ( $class, @argv ) = @_;
my %opt = (
original_argv => \@argv,
args => [],
yes => 0,
force => 0,
quiet => 0,
D => [],
U => [],
A => [],
sitecustomize => '',
destdir => '',
noman => '',
variation => '',
both => [],
append => '',
reverse => 0,
verbose => 0,
);
$opt{$_} = '' for keys %flavor;
if (@argv) {
# build a local @ARGV to allow us to use an older
# Getopt::Long API in case we are building on an older system
local (@ARGV) = @argv;
Getopt::Long::Configure(
'pass_through',
'no_ignore_case',
'bundling',
'permute', # default behaviour except 'exec'
);
$class->parse_cmdline( \%opt );
$opt{args} = \@ARGV;
# fix up the effect of 'bundling'
foreach my $flags ( @opt{qw(D U A)} ) {
foreach my $value ( @{$flags} ) {
$value =~ s/^=//;
}
}
}
my $self = bless \%opt, $class;
# Treat --root option same way as env variable PERLBREW_ROOT (with higher priority)
if ( $opt{root} ) {
$ENV{PERLBREW_ROOT} = $self->root( $opt{root} );
}
if ( $opt{builddir} ) {
$self->{builddir} = App::Perlbrew::Path->new( $opt{builddir} );
}
# Ensure propagation of $PERLBREW_HOME and $PERLBREW_ROOT
$self->root;
$self->home;
if ( $self->{verbose} ) {
$App::Perlbrew::HTTP::HTTP_VERBOSE = 1;
}
return $self;
}
sub parse_cmdline {
my ( $self, $params, @ext ) = @_;
my @f = map { $flavor{$_}{opt} || $_ } keys %flavor;
return Getopt::Long::GetOptions(
$params,
'yes',
'force|f',
'reverse',
'notest|n',
'quiet|q',
'verbose|v',
'input|i=s',
'output|o=s',
'as=s',
'append=s',
'help|h',
'version',
'root=s',
'switch',
'all',
'shell=s',
'no-patchperl',
'no-decoration',
"builddir=s",
# options passed directly to Configure
'D=s@',
'U=s@',
'A=s@',
'j=i',
# options that affect Configure and customize post-build
'sitecustomize=s',
'destdir=s',
'noman',
# flavors support
'both|b=s@',
'all-variations',
'common-variations',
@f,
@ext
);
}
sub sys { App::Perlbrew::Sys:: }
sub root {
my ( $self, $new_root ) = @_;
$new_root ||=
$PERLBREW_ROOT
|| $ENV{PERLBREW_ROOT}
|| App::Perlbrew::Path->new( $ENV{HOME}, "perl5", "perlbrew" )->stringify
unless $self->{root};
$self->{root} = $PERLBREW_ROOT = $new_root
if defined $new_root;
$self->{root} = App::Perlbrew::Path::Root->new( $self->{root} )
unless ref $self->{root};
$self->{root} = App::Perlbrew::Path::Root->new( $self->{root}->stringify )
unless $self->{root}->isa('App::Perlbrew::Path::Root');
return $self->{root};
}
sub home {
my ( $self, $new_home ) = @_;
$new_home ||=
$PERLBREW_HOME
|| $ENV{PERLBREW_HOME}
|| App::Perlbrew::Path->new( $ENV{HOME}, ".perlbrew" )->stringify
unless $self->{home};
$self->{home} = $PERLBREW_HOME = $new_home
if defined $new_home;
$self->{home} = App::Perlbrew::Path->new( $self->{home} )
unless ref $self->{home};
return $self->{home};
}
sub builddir {
my ($self) = @_;
return $self->{builddir} || $self->root->build;
}
sub current_perl {
my ( $self, $v ) = @_;
$self->{current_perl} = $v if $v;
return $self->{current_perl} || $self->env('PERLBREW_PERL') || '';
}
sub current_lib {
my ( $self, $v ) = @_;
$self->{current_lib} = $v if $v;
return $self->{current_lib} || $self->env('PERLBREW_LIB') || '';
}
sub current_shell_is_bashish {
my ($self) = @_;
return ( $self->current_shell eq 'bash' ) || ( $self->current_shell eq 'zsh' );
}
sub current_shell {
my ( $self, $x ) = @_;
$self->{current_shell} = $x if $x;
return $self->{current_shell} ||= do {
my $shell_name = App::Perlbrew::Path->new( $self->{shell} || $self->env('SHELL') )->basename;
$shell_name =~ s/\d+$//;
$shell_name;
};
}
sub current_env {
my ($self) = @_;
my $l = $self->current_lib;
$l = "@" . $l if $l;
return $self->current_perl . $l;
}
sub installed_perl_executable {
my ( $self, $name ) = @_;
die unless $name;
my $executable = $self->root->perls($name)->perl;
return $executable if -e $executable;
return "";
}
sub configure_args {
my ( $self, $name ) = @_;
my $perl_cmd = $self->installed_perl_executable($name);
my $code = 'while(($_,$v)=each(%Config)){print"$_ $v" if /config_arg/}';
my @output = split "\n" => $self->do_capture( $perl_cmd, '-MConfig', '-wle', $code );
my %arg;
for (@output) {
my ( $k, $v ) = split " ", $_, 2;
$arg{$k} = $v;
}
if (wantarray) {
return map { $arg{"config_arg$_"} } ( 1 .. $arg{config_argc} );
}
return $arg{config_args};
}
sub cpan_mirror {
my ( $self, $v ) = @_;
$self->{cpan_mirror} = $v if $v;
unless ( $self->{cpan_mirror} ) {
$self->{cpan_mirror} = $self->env("PERLBREW_CPAN_MIRROR") || "https://cpan.metacpan.org";
$self->{cpan_mirror} =~ s{/+$}{};
}
return $self->{cpan_mirror};
}
sub env {
my ( $self, $name ) = @_;
return $ENV{$name} if $name;
return \%ENV;
}
sub is_shell_csh {
my ($self) = @_;
return 1 if $self->env('SHELL') =~ /(t?csh)/;
return 0;
}
# Entry point method: handles all the arguments
# and dispatches to an appropriate internal
# method to execute the corresponding command.
sub run {
my ($self) = @_;
$self->run_command( $self->args );
}
sub args {
my ($self) = @_;
# keep 'force' and 'yes' coherent across commands
$self->{force} = $self->{yes} = 1 if ( $self->{force} || $self->{yes} );
return @{ $self->{args} };
}
sub commands {
my ($self) = @_;
my $package = ref $self ? ref $self : $self;
my @commands;
my $symtable = do {
no strict 'refs';
\%{ $package . '::' };
};
foreach my $sym ( keys %$symtable ) {
if ( $sym =~ /^run_command_/ ) {
my $glob = $symtable->{$sym};
if ( ref($glob) eq 'CODE' || defined *$glob{CODE} ) {
# with perl >= 5.27 stash entry can points to a CV directly
$sym =~ s/^run_command_//;
$sym =~ s/_/-/g;
push @commands, $sym;
}
}
}
return @commands;
}
sub find_similar_commands {
my ( $self, $command ) = @_;
$command =~ s/_/-/g;
return @{ find_similar_tokens($command, [ sort $self->commands ]) };
}
# This method is called in the 'run' loop
# and executes every specific action depending
# on the type of command.
#
# The first argument to this method is a self reference,
# while the first "real" argument is the command to execute.
# Other parameters after the command to execute are
# considered as arguments for the command itself.
#
# In general the command is executed via a method named after the