打印语句在 perl 的外部块中打印两次

Print statement prints twice in outer block in perl

以下是改编自 this link 的代码,用于获取天气并在 i3blocks 上显示。

#!/bin/bash 

METRIC=1 #Should be 0 or 1; 0 for F, 1 for C

if [ -z  ]; then
    echo
    echo "USAGE: weather.sh <locationcode>"
    echo
    exit 0;
fi

curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\= | perl -ne 'use utf8;

if ( /Currently/ ) {

    chomp;
    /\<title\>Currently: (.*)?\<\/title\>/;

    my @values  = split(":", ); 
    my $deg     = "°C";
    my @values2 = split("C", $values[1]);

    if ( $values[0] eq "Sunny" || $values[0] eq "Mostly Sunny" || $values[0] eq "Partly Sunny" || $values[0] eq "Intermittent Clouds" || $values[0] eq "Hazy Sunshine" || $values[0] eq "Hazy Sunshine" || $values[0] eq "Hot") {
        my $sun = "";
        binmode(STDOUT, ":utf8");
        print "$sun";
    }

    if ( $values[0] eq "Mostly Cloudy" || $values[0] eq "Cloudy" || $values[0] eq "Dreary (Overcast)" || $values[0] eq "Fog" ) {
        my $cloud = "";
        binmode(STDOUT, ":utf8");
        print "$cloud";
    }

    if ( $values[0] eq "Showers" || $values[0] eq "Mostly Cloudy w/ Showers" || $values[0] eq "Partly Sunny w/ Showers" || $values[0] eq "T-Storms"|| $values[0] eq "Mostly Cloudy w/ T-Storms"|| $values[0] eq "Partly Sunny w/ T-Storms"|| $values[0] eq "Rain" ) {
        my $rain = "";
        binmode(STDOUT, ":utf8");
        print "$rain";
    }

    if ( $values[0] eq "Windy" ) {
        my $wind = "";
        binmode(STDOUT, ":utf8");
        print "$wind";
    }

    if ( $values[0] eq "Flurries" || $values[0] eq "Mostly Cloudy w/ Flurries" || $values[0] eq "Partly Sunny w/ Flurries"|| $values[0] eq "Snow"|| $values[0] eq "Mostly Cloudy w/ Snow"|| $values[0] eq "Ice"|| $values[0] eq "Sleet"|| $values[0] eq "Freezing Rain"|| $values[0] eq "Rain and Snow"|| $values[0] eq "Cold" ) {
        my $snow = "";
        binmode(STDOUT, ":utf8");
        print "$snow";
    }

    if ( $values[0] eq "Clear" || $values[0] eq "Mostly Clear" || $values[0] eq "Partly Cloudy"|| $values[0] eq "Intermittent Clouds"|| $values[0] eq "Hazy Moonlight"|| $values[0] eq "Mostly Cloudy"|| $values[0] eq "Partly Cloudy w/ Showers"|| $values[0] eq "Mostly Cloudy w/ Showers"|| $values[0] eq "Partly Cloudy w/ T-Storms"|| $values[0] eq "Mostly Cloudy w/ Flurries" || $values[0] eq "Mostly Cloudy w/ Snow" ) {
        my $night = "";
        binmode(STDOUT, ":utf8");
        print "$night";
    }

    binmode(STDOUT, ":utf8");
    print  "$values2[0]$deg"; 
}'

(可能看起来像方框的文本来自 fontawesome 字体)。我添加的只是以下几行

my $deg = "°C";
my @values2 = split("C",$values[1]);

并修改了原为print "$values[1]"的最后一个print语句。旧的 print 语句显示没有度数符号的温度,如 35C。我的意图只是在 35°C 之间添加度数,但输出是

35°C°C

为什么子字符串打印了两次?即使我将它包含在单独的 print 语句中或直接包含子字符串 (print $values2[0]°C),也会发生这种情况。

奇怪的是,在 print "$sun$values2[0]$deg"; 等内部块中包含值和单位似乎没有重复就可以正常工作。

该脚本通过在 RSS 中查找 <title> 标签来工作。如果您手动执行此操作,您会看到有多个。'

curl -s 'http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=ASI|IN|IN031|MADRAS' | grep title
<title>Madras, IN - AccuWeather.com Forecast</title>
            <title>Madras, IN - AccuWeather.com Forecast</title>
            <title>Currently: Partly Sunny: 35C</title> 
                <title>5/23/2018 Forecast</title>
                <title>5/24/2018 Forecast</title>
<title>The AccuWeather.com RSS Center</title>

如果你添加

use strict;
use warnings;

你会收到大量关于未定义变量的警告。那是因为它没有找到 title 与该模式的匹配项,但它仍然打印。

  if (/Currently/) {
    chomp;
    /\<title\>Currently: (.*)?\<\/title\>/;

    # ....
    print "$values2[0]$deg";
  }

这也是您获得额外输出的原因。

如果没有匹配项,您需要跳过该行。

/\<title\>Currently: (.*)?\<\/title\>/ or next;