代码之家  ›  专栏  ›  技术社区  ›  Aakash Goel

将一行Perl代码分成两行的正确方法是什么?

  •  26
  • Aakash Goel  · 技术社区  · 14 年前
    $ cat temp.pl
    use strict;
    use warnings;
    
    print "1\n";
    print "hello, world\n";
    
    print "2\n";
    print "hello,
    world\n";
    
    print "3\n";
    print "hello, \
    world\n";
    
    $ perl temp.pl
    1
    hello, world
    2
    hello,
    world
    3
    hello, 
    world
    $
    

    为了使代码易于阅读,我想将列数限制为80个字符。如何将一行代码分成两行而不产生任何副作用?

    如上图所示,一个简单的 艾斯 \ 不起作用。

    正确的方法是什么?

    4 回复  |  直到 9 年前
        1
  •  42
  •   Ether    14 年前

    在Perl中,在常规空间中的任何地方都可以使用回车。反斜杠不像某些语言那样使用;只需添加 .

    可以使用串联或列表操作在多行上拆分字符串:

    print "this is ",
        "one line when printed, ",
        "because print takes multiple ",
        "arguments and prints them all!\n";
    print "however, you can also " .
        "concatenate strings together " .
        "and print them all as one string.\n";
    
    print <<DOC;
    But if you have a lot of text to print,
    you can use a "here document" and create
    a literal string that runs until the
    delimiter that was declared with <<.
    DOC
    print "..and now we're back to regular code.\n";
    

    您可以在此处阅读文档 perldoc perlop .

        2
  •  11
  •   Nikhil Jain    14 年前

    还有一件事 Perl Best Practices :

    断开长线: 在运算符前中断长表达式。 喜欢

    push @steps, $step[-1]
                      + $radial_velocity * $elapsed_time
                      + $orbital_velocity * ($phrase + $phrase_shift)
                      - $test
                      ; #like that
    
        3
  •  4
  •   codaddict    14 年前

    这是因为你在一个字符串中。可以拆分字符串并使用 . AS:

    print "3\n";
    print "hello, ".
    "world\n";
    
        4
  •  1
  •   pjvandehaar imgx64    9 年前

    使用 . ,字符串串联运算符:

    $ perl
    print "hello, " .
    "world\n";ctrl-d
    hello, world
    $