代码之家  ›  专栏  ›  技术社区  ›  Rob Wells

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

  •  4
  • Rob Wells  · 技术社区  · 16 年前

    G'Day.

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

    参数“ww4.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 回复  |  直到 16 年前
        1
  •  12
  •   Paul Dixon    16 年前

    第56行可能是后面的一行 试着把两根弦分开。你可能的目的是

       foreach my $dir (@log_dirs) {
            foreach my $log (@log_list) {
                if ( -s "$dir/$log" ) {
                    push(@logs, "$dir/$log");
                }
            }
        }
    
        2
  •  9
  •   brian d foy    14 年前

    你没有引用你的字符串 push . 与其自己创建路径,不如尝试养成使用 File::Spec :

    use File::Spec::Functions;
    
    my $path = catfile( $dir, $file );
    

    然后你使用 $path 无论何时,只要你想要这个字符串,这样你就不必通过重新创建字符串来让自己重复(也许下次会出错;)。

        3
  •  5
  •   1800 INFORMATION    16 年前

    这是由以下几行引起的:

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

    你在那里有一个部门。

        4
  •  2
  •   ysth    16 年前

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