Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 mean something like:

my $dbh = DBI->connect("DBI:mysql:database=xxxxxxx;host=xxxxxt", "xxxx", "xxxxxx", \%dbattr) or die ("Bla bla bla bla");
like image 231
user943523 Avatar asked Nov 29 '22 17:11

user943523


2 Answers

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.

like image 95
Matteo Avatar answered Dec 05 '22 07:12

Matteo


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");
like image 45
Konerak Avatar answered Dec 05 '22 07:12

Konerak