您可以改进这个命令行 Perl 脚本来查找系统上的所有 Perl 安装吗?

问题描述 投票:0回答:1
#/usr/bin/env perl
# Last modified: Sat Aug 24 2024 10:48:44 PM -04:00 [EDT]
#
use File::Which;
use File::Basename;
use File::stat;
use Digest::MD5;
# use Data::Dump qw/dump/;

use warnings;

=head1 Installation

C<chmod 700 perlwitch> - on GNU/Linux and similar *nix-ish systems

=head1 Usage

C<perl perlwitch>

=cut

my @heap = which( 'perl' );
my $trimmed;
my $struct; my $index = 0;
my @versions;

print "We are running [$^X] on $^O which is perl $^V\n";

if (($^O eq 'cygwin') or ($^O eq 'MSWin32')) {
    no warnings;
    my $pn;
    my $i = 0;
    for $pn (@heap) {
        unless ( $pn =~m/(\.exe)$/i ) {
        splice (@heap, $i, 1);
        } else {
        #   print "\nItem $i Not removed from data: matched $1 :", $pn, qq[\n];
        $i++
        }
    }
}

$index = 0;
my $strl = 0; my $s = 0; my $first = 0;
for $s (@heap) {
  if (length($s) >= $strl) { $strl =  length $s; }
      open READPIPE, '-|', $s, '-v' || warn "Call a plumber! Pipe broken on attempted fork!\n  $!";
      while (<READPIPE>) {
            if ($_ =~m/This is perl .+(v[\.[[:digit:]]+)\) /) { push @versions, ($1) }
      }
      close READPIPE || warn "Got a bad close: $?\n";
}

my $fmt  = $strl + 6;

for $path (@heap) {
     $struct->[$index]->[0] = $path;
     $struct->[$index]->[1] = $versions[$index];
     my $arro = csum($path);
     my $st = stat($path);
     my $bytesize = $st->size;
     $struct->[$index]->[2] = $bytesize;

     my (undef, $dir, undef) = fileparse($path);
     $struct->[$index]->[3]  = $dir;
     $struct->[$index]->[4]  = $arro;
     $trimmed = $strl - 4;
     print sprintf(qq/%-${fmt}s/, $path), $versions[$index], q[ ], $bytesize, q[ ],
        q[ ], sprintf(qq/%-${trimmed}s/, $dir), q[ ], sprintf("%36s", $arro), qq[\n];
     $index++;
}

# print dump $struct; print qq[\n];

sub csum {
# we pass in a fqual filename
    my $fpath = shift @_;
    open( my $fhandl, '<',  $fpath ) || warn "Failed open on file $fpath: $!" && return q[];
    binmode( $fhandl );
    my $check = Digest::MD5->new->addfile($fhandl)->hexdigest;
    close( $fhandl );
    return $check;
}

__END__

=pod

The purpose of this program is to locate all installations of Perl on a system that are locateable
in the system $PATH. It might be a box you haven't used for  while, or perhaps one you
are unfamiliar with. It will list these with a checksum for each, making it easy to see if there
are any duplicates (such as might be found where some symlink sorcery is involved, like linking F</bin> to
F</usr/bin>. The widely used (and wisely used) Debian GNU/Linux now does this, for example.

=head3 Sample output from a complex case: cygwin and MSWin32 perls on the same system

    We are running [C:\perl\perl\bin\perl.exe] on MSWin32 which is perl v5.32.1
    C:\ix\cygwin\bin\perl.EXE      v5.36.3 12819  C:\ix\cygwin\bin\         68ba8b0c3d9bb4859076e938e0cbdafe
    C:\perl\perl\bin\perl.EXE      v5.32.1 39936  C:\perl\perl\bin\         3686d8a7e98b82a6452f88fef293ca1a

=head3 TODO

Get script working on cygwin, which is not displaying correctly.

=head2 License

Copyright (c) 2024 Soren Andersen. All rights reserved.

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut
enter code here
cygwin perl system-administration
1个回答
0
投票

在类似 Linux 的环境中,您应该有

type
可用。

$ type -a perl
perl is /usr/bin/perl
perl is /bin/perl

...这样你就可以做一个更简单的检查,
或者诉诸运行脚本中显示较短版本号的部分:

$ type -a perl | cut -d' ' -f3 | while read name ;\  
  do echo -e "\n\n[ --- $name --- ]"; $name --version ;done


[ --- /usr/bin/perl --- ]

This is perl 5, version 38, subversion 2 (v5.38.2) built for x86_64-linux-gnu-thread-multi
(with 44 registered patches, see perl -V for more detail)

Copyright 1987-2023, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at https://www.perl.org/, the Perl Home Page.



[ --- /bin/perl --- ]

This is perl 5, version 38, subversion 2 (v5.38.2) built for x86_64-linux-gnu-thread-multi
(with 44 registered patches, see perl -V for more detail)

Copyright 1987-2023, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at https://www.perl.org/, the Perl Home Page.

$
© www.soinside.com 2019 - 2024. All rights reserved.