为什么这个预期的插值被 Perl 解释为除法?

发布于 2024-07-25 04:17:03 字数 1316 浏览 9 评论 0原文

天哪,

为什么我从下面的脚本片段中收到以下两个错误?

参数“www4.mh.xxxx.co.uk.logstatsto20090610.gz”在第 56 行的除号 (/) 中不是数字

参数“/logs/xxxx/200906/mcs0.telhc/borg2”在第 56 行的除法 (/) 中不是数字

变量 $dir 和 $log 都是字符串,两个字符串的串联以及中间的斜杠也用引号引起来。

        foreach my $dir (@log_dirs) {
            foreach my $log (@log_list) {
line 56:        if ( -s "$dir/$log" ) {
                    push(@logs, $dir/$log);
                }
            }
        }

编辑:第 56 行绝对是 if 语句。 然而,Paul,你是对的,用引号将第 57 行的除法括起来可以解决问题。 谢谢。

编辑:报告第 56 行的 Perl 版本是

stats@fs1:/var/tmp/robertw> /usr/local/perl/bin/perl -v      

This is perl, v5.6.1 built for sun4-solaris

Copyright 1987-2001, 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 http://www.perl.com/, the Perl Home Page.

stats@fs1:/var/tmp/robertw> 

编辑:尽管使用 Perl 中插值字符串的方法,但考虑到变量本身就是字符串,并且我试图将它们连接在一起带有斜杠字符不是最终结果字符串连接吗?

干杯,

G'day,

Why am I getting the following two errors from the script fragment below?

Argument "www4.mh.xxxx.co.uk.logstatsto20090610.gz" isn't numeric in division (/) at line 56

Argument "/logs/xxxx/200906/mcs0.telhc/borg2" isn't numeric in division (/) at line 56

The variables $dir and $log are both strings and the concatenation of the two strings, along with the slash in the middle, is also wrapped with quotation marks.

        foreach my $dir (@log_dirs) {
            foreach my $log (@log_list) {
line 56:        if ( -s "$dir/$log" ) {
                    push(@logs, $dir/$log);
                }
            }
        }

Edit: Line 56 is definitely the if statement. However, Paul, you're right, surrounding the division on line 57 with quotation marks fixes the problem. Thanks.

Edit: The Perl version reporting Line 56 is

stats@fs1:/var/tmp/robertw> /usr/local/perl/bin/perl -v      

This is perl, v5.6.1 built for sun4-solaris

Copyright 1987-2001, 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 http://www.perl.com/, the Perl Home Page.

stats@fs1:/var/tmp/robertw> 

Edit: Though using the method of interpolated strings in Perl, given that the variables are themselves strings and I am attempting to join them together with a slash character isn't the net result string concatenation?

cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔少女心 2024-08-01 04:17:03

第 56 行可能是它之后的行,您在其中尝试划分两个字符串。 你的意图可能是

   foreach my $dir (@log_dirs) {
        foreach my $log (@log_list) {
            if ( -s "$dir/$log" ) {
                push(@logs, "$dir/$log");
            }
        }
    }

Line 56 is probably the line after it, where you do try to divide two strings. What your probably intended was

   foreach my $dir (@log_dirs) {
        foreach my $log (@log_list) {
            if ( -s "$dir/$log" ) {
                push(@logs, "$dir/$log");
            }
        }
    }
故人的歌 2024-08-01 04:17:03

您没有在 push 中引用该字符串。 不要自己创建路径,而是尝试养成使用 File::Spec

use File::Spec::Functions;

my $path = catfile( $dir, $file );

然后,只要您需要该字符串,就可以使用 $path ,这样您就不必通过再次重新创建该字符串来重复自己(并且下次可能会做错;)。

You don't quote the string in your push. Instead of creating paths yourself, try to get into the habit of making portable paths with File::Spec:

use File::Spec::Functions;

my $path = catfile( $dir, $file );

Then you use $path whenever you want that string so you don't have the repeat yourself by remaking the string again (and perhaps doing it wrong the next time ;).

染墨丶若流云 2024-08-01 04:17:03

这是由以下行引起的:

push(@logs, $dir/$log);

你那里有一个部门。

It's caused by the following line:

push(@logs, $dir/$log);

You have a division there.

当梦初醒 2024-08-01 04:17:03

我很想知道你使用的 perl 版本是什么? 我可以轻松尝试的所有方法都可以正确报告推送的行号。

I'm curious to know what version of perl you are using? All the ones I can easily try are correctly reporting the line number of the push.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文