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

时间:2009-06-11 12:34:28

标签: perl string-interpolation

天儿真好,

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

  

参数“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语句。但是,保罗,你是对的,用引号围绕第57行的分区来解决问题。感谢。

编辑:Perl版本报告第56行

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中使用了插值字符串的方法,但是假设变量本身是字符串而我试图将它们与斜杠字符连接在一起并不是净结果字符串连接?

欢呼声,

4 个答案:

答案 0 :(得分:12)

第56行可能是它之后的行,你试图划分两个字符串。你可能想要的是什么

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

答案 1 :(得分:9)

您不引用push中的字符串。不要自己创建路径,而是要养成使用File::Spec创建可移植路径的习惯:

use File::Spec::Functions;

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

然后,只要你想要那个字符串,就可以使用$path,这样你就不会再次重新设置字符串了(并且下次可能做错了。)。

答案 2 :(得分:5)

由以下行引起:

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

你在那里有分工。

答案 3 :(得分:2)

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