<commit_before>package App::perlbrew;
use strict;

our $VERSION = "0.01";

my $ROOT = $ENV{PERLBREW_ROOT} || "$ENV{HOME}/perl5/perlbrew";
my $CURRENT_PERL = "$ROOT/perls/current";

sub run_command {
    my ( undef, $opt, $x, @args ) = @_;
    $opt->{log_file} = "$ROOT/build.log";
    my $self = bless $opt, __PACKAGE__;
    $x ||= "help";
    my $s = $self->can("run_command_$x") or die "Unknow command: `$x`. Typo?";
    $self->$s(@args);
}

sub run_command_help {
    print <<HELP;

Usage:

    perlbrew init
    perlbrew install perl-5.11.1
    perlbrew installed
    perlbrew switch perl-5.11.1

HELP
}

sub run_command_init {
    require File::Path;
    File::Path::make_path(
        "$ROOT/perls", "$ROOT/dists", "$ROOT/build", "$ROOT/etc",
        "$ROOT/bin"
    );

    system <<"RC";
echo 'export PATH=$ROOT/perls/bin:$CURRENT_PERL/bin:\${PATH}' > $ROOT/etc/bashrc
RC
    my $local_lib;
    eval { require local::lib };
    unless ($@) {
        local::lib->import($CURRENT_PERL);
        system <<"RC";
echo 'eval \$(perl -I\$HOME/perl5/lib/perl5 -Mlocal::lib=$CURRENT_PERL)' >> $ROOT/etc/bashrc
RC

        $local_lib = 1;
    }

    print "Perlbrew environmet Initiated.";
    print "Required directories are created under $ROOT.";
    print "Please add this to the end of your ~/.bashrc:";
    print "    source $ROOT/etc/bashrc";
    if ($local_lib) {
        print "local::lib found and automatically configured";
        print "you will need to remove the existing local::lib config if any";
    }
}

sub run_command_install {
    my ( $self, $dist, $opts ) = @_;

    my ( $dist_name, $dist_version ) = $dist =~ m/^(.*)-([\d.]+)$/;
    if ( $dist_name eq 'perl' ) {
        require LWP::UserAgent;
        my $ua = LWP::UserAgent->new;

        print "Fetching $dist...";
        my $r = $ua->get("http://search.cpan.org/dist/$dist");
        die "Fail to fetch the dist of $dist." unless $r->is_success;

        my $html = $r->content;
        my ( $dist_path, $dist_tarball ) = $html =~
m[<a href="(/CPAN/authors/id/.+/(${dist}.tar.(gz|bz2)))">Download</a>];

        $r = $ua->get(
            "http://search.cpan.org/${dist_path}",
            ":content_file" => "$ROOT/dists/${dist_tarball}"
        );
        die "Fail to fetch the dist of $dist." unless $r->is_success;
        my @d_options = @{ $self->{D} };
        unshift @d_options, qq(prefix=$ROOT/perls/$dist);
        push @d_options, "usedevel" if $dist_version =~ /5\.11/;
        print "Installing $dist...";
        my $tarx = "tar " . ( $dist_tarball =~ /bz2/ ? "xjf" : "xzf" );

        my $cmd = join ";",
          (
            "cd $ROOT/build",
            "$tarx $ROOT/dists/${dist_tarball}",
            "cd $dist",
            "rm -f config.sh Policy.sh",
            "sh Configure -de " . join( ' ', map { "-D$_" } @d_options ),
            "make",
            (
                $self->{force}
                ? ( 'make test', 'make install' )
                : "make test && make install"
            )
          );
        $cmd = "($cmd) >> '$self->{log_file}' 2>&1 "
          if ( $self->{quiet} && !$self->{verbose} );
        system($cmd);
    }
}

sub run_command_installed {
    my $self = shift;
    for (<$ROOT/perls/perl-*>) {
        my ($name) = $_ =~ m/(perl-.+)$/;
        print "$name";
    }
}

sub run_command_switch {
    my ( $self, $dist ) = @_;
    die "${dist} is not installed\n" unless -d "$ROOT/perls/${dist}";
    my ( $dist_name, $dist_version ) = $dist =~ m/^(.*)-([\d.]+)$/;
    unlink "$CURRENT_PERL";
    system "cd $ROOT/perls; ln -s $dist current";
    for my $executable (<$CURRENT_PERL/bin/*${dist_version}>) {
        my ($name) = $executable =~ m/bin\/(.+)${dist_version}/;
        system("ln -fs $executable $ROOT/bin/${name}");
    }
}

1;

__END__

=head1 NAME

perlbrew - Perl Environment manager.

=head1 SYNOPSIS

    # Initialize
    perlbrew init

    # Install some Perls
    perlbrew install perl-5.8.1<commit_msg>roll back a change in run_command_switch; update config notes in run_command_init<commit_after>package App::perlbrew;
use strict;

our $VERSION = "0.01";

my $ROOT = $ENV{PERLBREW_ROOT} || "$ENV{HOME}/perl5/perlbrew";
my $CURRENT_PERL = "$ROOT/perls/current";

sub run_command {
    my ( undef, $opt, $x, @args ) = @_;
    $opt->{log_file} = "$ROOT/build.log";
    my $self = bless $opt, __PACKAGE__;
    $x ||= "help";
    my $s = $self->can("run_command_$x") or die "Unknow command: `$x`. Typo?";
    $self->$s(@args);
}

sub run_command_help {
    print <<HELP;

Usage:

    perlbrew init
    perlbrew install perl-5.11.1
    perlbrew installed
    perlbrew switch perl-5.11.1

HELP
}

sub run_command_init {
    require File::Path;
    File::Path::make_path(
        "$ROOT/perls", "$ROOT/dists", "$ROOT/build", "$ROOT/etc",
        "$ROOT/bin"
    );

    system <<"RC";
echo 'export PATH=$ROOT/perls/bin:$CURRENT_PERL/bin:\${PATH}' > $ROOT/etc/bashrc
RC
    my $local_lib;
    eval { require local::lib };
    unless ($@) {
        local::lib->import($CURRENT_PERL);
        system <<"RC";
echo 'eval \$(perl -I\$HOME/perl5/lib/perl5 -Mlocal::lib=$CURRENT_PERL)' >> $ROOT/etc/bashrc
RC

        $local_lib = 1;
    }

    print "Perlbrew environmet Initiated.";
    print "Required directories are created under $ROOT.";
    print "Please add this to the end of your ~/.bashrc:";
    print "    source $ROOT/etc/bashrc";
    if ($local_lib) {
        print "local::lib found and automatically configured";
        print "Please be sure you put the perlbrew configuration *after* any local::lib configuration:";
		print '    eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)';
		print "    source $ROOT/etc/bashrc";
    }
}

sub run_command_install {
    my ( $self, $dist, $opts ) = @_;

    my ( $dist_name, $dist_version ) = $dist =~ m/^(.*)-([\d.]+)$/;
    if ( $dist_name eq 'perl' ) {
        require LWP::UserAgent;
        my $ua = LWP::UserAgent->new;

        print "Fetching $dist...";
        my $r = $ua->get("http://search.cpan.org/dist/$dist");
        die "Fail to fetch the dist of $dist." unless $r->is_success;

        my $html = $r->content;
        my ( $dist_path, $dist_tarball ) = $html =~
m[<a href="(/CPAN/authors/id/.+/(${dist}.tar.(gz|bz2)))">Download</a>];

        $r = $ua->get(
            "http://search.cpan.org/${dist_path}",
            ":content_file" => "$ROOT/dists/${dist_tarball}"
        );
        die "Fail to fetch the dist of $dist." unless $r->is_success;
        my @d_options = @{ $self->{D} };
        unshift @d_options, qq(prefix=$ROOT/perls/$dist);
        push @d_options, "usedevel" if $dist_version =~ /5\.11/;
        print "Installing $dist...";
        my $tarx = "tar " . ( $dist_tarball =~ /bz2/ ? "xjf" : "xzf" );

        my $cmd = join ";",
          (
            "cd $ROOT/build",
            "$tarx $ROOT/dists/${dist_tarball}",
            "cd $dist",
            "rm -f config.sh Policy.sh",
            "sh Configure -de " . join( ' ', map { "-D$_" } @d_options ),
            "make",
            (
                $self->{force}
                ? ( 'make test', 'make install' )
                : "make test && make install"
            )
          );
        $cmd = "($cmd) >> '$self->{log_file}' 2>&1 "
          if ( $self->{quiet} && !$self->{verbose} );
        system($cmd);
    }
}

sub run_command_installed {
    my $self = shift;
    for (<$ROOT/perls/perl-*>) {
        my ($name) = $_ =~ m/(perl-.+)$/;
        print "$name";
    }
}

sub run_command_switch {
    my ( $self, $dist ) = @_;
    die "${dist} is not installed\n" unless -d "$ROOT/perls/${dist}";
    my ( $dist_name, $dist_version ) = $dist =~ m/^(.*)-([\d.]+)$/;
	unlink "$ROOT/perls/current";
    system "cd $ROOT/perls; ln -s $dist current";
	for my $executable (<$ROOT/perls/current/bin/*${dist_version}>) {
        my ($name) = $executable =~ m/bin\/(.+)${dist_version}/;
        system("ln -fs $executable $ROOT/bin/${name}");
    }
}

1;

__END__

=head1 NAME

perlbrew - Perl Environment manager.

=head1 SYNOPSIS

    # Initialize
    perlbrew init

    # Install some Perls
    perlbrew install perl-5.8.1
{'fraction_non_alphanumeric': 0.19035907525823906, 'fraction_numerical': 0.007132316773241515, 'mean_word_length': 2.302070645554202, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3994420', 'n_tokens_mistral': 3029, 'n_tokens_neox': 2912, 'n_words': 668}