| 1 | package Perl::Exactly; | ||
| 2 | ###################################################################### | ||
| 3 | # | ||
| 4 | # Perl::Exactly - ensures exactly version of perl | ||
| 5 | # | ||
| 6 | # http://search.cpan.org/dist/Perl-Exactly/ | ||
| 7 | # | ||
| 8 | # Copyright (c) 2014, 2015, 2017, 2018, 2019, 2023 INABA Hitoshi <ina@cpan.org> in a CPAN | ||
| 9 | ###################################################################### | ||
| 10 | |||
| 11 | $VERSION = '0.07'; | ||
| 12 | $VERSION = $VERSION; | ||
| 13 | |||
| 14 | use 5.00503; | ||
| 15 | use strict; | ||
| 16 | BEGIN { $INC{'warnings.pm'} = '' if $] < 5.006 }; use warnings; $^W=1; | ||
| 17 | |||
| 18 | if ($0 eq __FILE__) { | ||
| 19 | print $]; | ||
| 20 | exit; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub VERSION { | ||
| 24 | my(undef, $exactly_perl_version) = @_; | ||
| 25 | |||
| 26 | if ($] !~ m/\A \Q$exactly_perl_version\E/oxms) { | ||
| 27 | if (not -e "@{[__FILE__]}.conf") { | ||
| 28 | _croak("@{[__FILE__]}: @{[__FILE__]}.conf not exists, stopped"); | ||
| 29 | } | ||
| 30 | |||
| 31 | # load configuration file | ||
| 32 | my %perlbin = %{ (do "@{[__FILE__]}.conf") || {} }; | ||
| 33 | |||
| 34 | if (not exists $perlbin{$exactly_perl_version}) { | ||
| 35 | _croak("Path of perl $exactly_perl_version not defined in @{[__FILE__]}.conf, stopped"); | ||
| 36 | } | ||
| 37 | elsif (not -e $perlbin{$exactly_perl_version}) { | ||
| 38 | _croak("$perlbin{$exactly_perl_version} in @{[__FILE__]}.conf not exists, stopped"); | ||
| 39 | } | ||
| 40 | elsif (`$perlbin{$exactly_perl_version} @{[__FILE__]}` !~ m/\A \Q$exactly_perl_version\E/oxms) { | ||
| 41 | _croak("$perlbin{$exactly_perl_version} isn't perl $exactly_perl_version, stopped"); | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | my @switch = (); | ||
| 45 | if ($^W) { | ||
| 46 | push @switch, '-w'; | ||
| 47 | } | ||
| 48 | if (defined $^I) { | ||
| 49 | push @switch, '-i' . $^I; | ||
| 50 | undef $^I; | ||
| 51 | } | ||
| 52 | |||
| 53 | # DOS-like system | ||
| 54 | if ($^O =~ m/\A (?: MSWin32 | NetWare | symbian | dos ) \z/oxms) { | ||
| 55 | exit _systemx( | ||
| 56 | _escapeshellcmd_MSWin32($perlbin{$exactly_perl_version}), | ||
| 57 | |||
| 58 | # -I switch can not treat space included path | ||
| 59 | # (map { '-I' . _escapeshellcmd_MSWin32($_) } @INC), | ||
| 60 | (map { '-I' . $_ } @INC), | ||
| 61 | |||
| 62 | @switch, | ||
| 63 | '--', | ||
| 64 | map { _escapeshellcmd_MSWin32($_) } $0, @ARGV | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | # UNIX-like system | ||
| 69 | else { | ||
| 70 | exit _systemx( | ||
| 71 | _escapeshellcmd($perlbin{$exactly_perl_version}), | ||
| 72 | (map { '-I' . _escapeshellcmd($_) } @INC), | ||
| 73 | @switch, | ||
| 74 | '--', | ||
| 75 | map { _escapeshellcmd($_) } $0, @ARGV | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | # escape shell command line on DOS-like system | ||
| 83 | sub _escapeshellcmd_MSWin32 { | ||
| 84 | my($word) = @_; | ||
| 85 | if ($word =~ m/ [ ] /oxms) { | ||
| 86 | return qq{"$word"}; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | return $word; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | # escape shell command line on UNIX-like system | ||
| 94 | sub _escapeshellcmd { | ||
| 95 | my($word) = @_; | ||
| 96 | return $word; | ||
| 97 | } | ||
| 98 | |||
| 99 | # safe system | ||
| 100 | sub _systemx { | ||
| 101 | $| = 1; | ||
| 102 | |||
| 103 | # local $ENV{'PATH'} = '.'; | ||
| 104 | local @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer | ||
| 105 | |||
| 106 | return CORE::system { $_[0] } @_; # safe even with one-argument list | ||
| 107 | } | ||
| 108 | |||
| 109 | # instead of Carp::croak | ||
| 110 | sub _croak { | ||
| 111 | my($package,$filename,$line) = caller(1); | ||
| 112 | print STDERR "@_ at $filename line $line.\n"; | ||
| 113 | die "\n"; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; | ||
| 117 | |||
| 118 | __END__ |