Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to break a line of Perl code into two?

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

To make my code easily readable, I want to restrict the number of columns to 80 characters. How can I break a line of code into two without any side effects?

As shown above, a simple or \ does not work.

What is the correct way to do this?

like image 445
Lazer Avatar asked Oct 21 '10 06:10

Lazer


People also ask

How do I split a value in Perl?

Perl | split() Function. split() is a string function in Perl which is used to split or you can say to cut a string into smaller sections or pieces. There are different criteria to split a string, like on a single character, a regular expression(pattern), a group of characters or on undefined value etc..

How do I split a string by space in Perl?

Perl Articles How can we split a string in Perl on whitespace? The simplest way of doing this is to use the split() function, supplying a regular expression that matches whitespace as the first argument.

How do I slice a string in Perl?

substr() in Perl returns a substring out of the string passed to the function starting from a given index up to the length specified. This function by default returns the remaining part of the string starting from the given index if the length is not specified.


1 Answers

In Perl, a carriage return will serve in any place where a regular space does. Backslashes are not used like in some languages; just add a CR.

You can break strings up over multiple lines with concatenation or list operations:

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

You can read about here documents in in perldoc perlop.

like image 62
Ether Avatar answered Oct 13 '22 06:10

Ether