Dave's Brain

Browse - programming tips - php perl heredoc

Date: 2010sep12
Updated: 2011sep26
Language: php, perl, C/C++, javaScript, Java, Python
Keywords: heredoc, inline text

Q.  What is the syntax for php and perl heredocs?
The syntax is close but different enough to drive me crazy.

In php:

		$var = <<<EOT
	line one
	line two
	$variables_work_here
	line three
	EOT;

In perl:

		$var = <<EOT;
	line one
	line two
	$variables_work_here
	line three
	EOT

In C/C++:
	There is no exact equivalent but you can take advantage of a trick:
	C/C++ will concatenate adjacent strings.

		var =
	"line one\n"
	"line two\n"
	"line three\n";

	Use sprintf() to put variables into a string.

In javaScript:
	Again, there is no heredoc.  But you can use a backslash for more
	compact multiline strings:

		var = 
	"line one\
	line two\
	line three";

	It would be great for JSON if javaScript supported a heredoc.

In Java:
	On Android use a string resource.

In Python:
	Use tripple double quotes:

	var = """
	line one
	line two
	I said "hello"
	last line
	"""
	
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: