如何解决"在C:/ Program Files / perl / lib / File / Spec / Win32.pm"中无效使用ucfirst在void上下文中?

时间:2016-03-02 16:12:49

标签: perl perl-module

#!/usr/bin/perl  -w
use strict;

use LWP::UserAgent;
use HTTP::Request;

my $idividi = "http://www.idividi.com.mk";
my $tocka = "http://tocka.com.mk";

sub check_server() {
  my @urlList = ($idividi, $tocka);
  print "Checking server availability...\n";

  foreach my $server_endpoint (@urlList){
     my $uaObject = LWP::UserAgent->new;
     my $request = HTTP::Request->new(GET => $server_endpoint);
     my $response = $uaObject->request($request);

     if(length($response->decoded_content) != 0) {
        print "$server_endpoint ---> HTTPS network connectivity:        AVAILABLE\n";
     } else {
        print "$server_endpoint ---> HTTPS network connectivity: NOT AVAILABLE\n";
     } 
 }

}

当我尝试运行时,我收到错误: "在C:/ Program Files / perl / lib / File / Spec / Win32.pm"中在void上下文中无用地使用ucfirst。

我在网上找不到任何可能的解决方案。

2 个答案:

答案 0 :(得分:1)

您的File/Spec/Win32.pm文件存在问题。您应该使用

从命令行重新安装整个File::Spec
cpan File::Spec

最新版本3.62在第385行有这么长的陈述

my $volume = $first =~ s{ \A ([A-Za-z]:) ([\\/]?) }{}x  # drive letter
           ? ucfirst( $1 ).( $2 ? "\\" : "" )
       : $first =~ s{ \A (?:\\\\|//) ([^\\/]+)
             (?: [\\/] ([^\\/]+) )?
                 [\\/]? }{}xs           # UNC volume
       ? "\\\\$1".( defined $2 ? "\\$2" : "" )."\\"
       : $first =~ s{ \A [\\/] }{}x         # root dir
       ? "\\"
       : "";

并且使用ucfirst无法导致无用的警告。


您可能也对代码的这种重构感兴趣,这避免了一些不太理想的构造

#!/usr/bin/perl

use strict;
use warnings 'all';

use LWP::UserAgent;
use URI;

my ( $idividi, $tocka ) = qw/ www.idividi.com.mk tocka.com.mk /;

check_servers( $idividi, $tocka );

sub check_servers {
    my @url_list = @_;

    print "Checking server availability...\n";

    my $ua  = LWP::UserAgent->new;
    my $url = URI->new('http://example.com/');

    for my $server (@url_list) {

        $url->host($server);
        my $response = $ua->get($url);
        my $avail = length( $response->decoded_content ) != 0;

        printf "%s ---> HTTPS network connectivity: %s\n",
            $server,
            $avail ? 'AVAILABLE' : 'NOT AVAILABLE';
    }
}

输出

Checking server availability...
www.idividi.com.mk ---> HTTPS network connectivity: AVAILABLE
tocka.com.mk ---> HTTPS network connectivity: AVAILABLE

答案 1 :(得分:0)

请勿使用perl -w。它可以在全局范围内启用警告 ,即使在可能未编写这些警告的模块中也是如此。

如果要启用警告,use warnings将为当前文件启用该警告。