Jaki jest prawidłowy sposób podziału linii kodu Perla na dwie?

$ 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
$

Aby mój kod był czytelny, chcę ograniczyć liczbę kolumn do 80 znaków. Jak mogę podzielić linię kodu na dwie bez żadnych skutków ubocznych?

Jak pokazano powyżej, Prosty lub \ nie działa.

Jaki jest właściwy sposób, aby to zrobić?

Author: Lazer, 2010-10-21

4 answers

W Perlu powrót karetki będzie służył w każdym miejscu, w którym robi to zwykła przestrzeń. Odwrotne ukośniki nie są używane jak w niektórych językach; wystarczy dodać CR .

Możesz rozbijać łańcuchy na wiele linii za pomocą operacji konkatenacji lub listy:

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";

Możesz przeczytać tutaj dokumenty w perldoc perlop .

 41
Author: Ether,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-21 06:28:23

Jeszcze jedno z Perl Best Practices :

Łamanie długich linii: łamanie długich wyrażeń przed operatorem. jak

push @steps, $step[-1]
                  + $radial_velocity * $elapsed_time
                  + $orbital_velocity * ($phrase + $phrase_shift)
                  - $test
                  ; #like that
 9
Author: Nikhil Jain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-21 06:56:16

To dlatego, że jesteś wewnątrz łańcucha. Możesz podzielić ciągi i połączyć za pomocą . jako:

print "3\n";
print "hello, ".
"world\n";
 4
Author: codaddict,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-10-21 06:22:37

Użyj ., operator konkatenacji ciągu:

$ perl
print "hello, " .
"world\n";ctrl-d
hello, world
$
 1
Author: imgx64,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-15 20:24:11