开发者

Avoid long lines in Perl scripts

I have used \ at the end of shell scripts to avoid long lines, and improve code readability. But I cannot do it in Perl, what am I doing wrong ? Do I have to escape them in any way?

I me开发者_开发技巧an something like:

my $dbh = DBI->connect("DBI:mysql:database=xxxxxxx;host=xxxxxt", "xxxx", "xxxxxx", \%dbattr) or die ("Bla bla bla bla");


Perl statements are terminated separated by a semicolon ;, you don't need to write code on one line. Your example can be formatted as:

my $dbh = DBI->connect(
  "DBI:mysql:database=xxxxxxx;host=xxxxxt",
  "xxxx",
  "xxxxxx",
  \%dbattr
) or
  die ("Bla bla bla bla");

Or in any way you like.

You can also take a look at tools like perltidy to automatically beautify your code.


There is no need to escape.

From perldoc perlsyn:

Perl is a free-form language, you can format and indent it however you like. Whitespace mostly serves to separate tokens, unlike languages like Python where it is an important part of the syntax:

my $dbh = 
 DBI->connect("DBI:mysql:database=xxxxxxx;host=xxxxxt", 
 "xxxx", "xxxxxx", \%dbattr
 ) 
 or die ("Bla bla bla bla");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜