

Discover more from hrbrmstr's Daily Drop
Drop #347 (2023-10-05): Here[doc], Ye; Here[doc] Ye
Heredocs Are Awesome; Bash/Zsh HereDoc Tutorial With Examples; Heredocs Galore
Tis a lighter edition, today, as it's been a very long week despite Thursday only just starting; and, I thought y'all might want a break from the daily resource bombardment. The [sub]titles kind of self-describe the topic of the day, so let's dive in to heredocs!
TL;DR
This is an AI-generated summary of today's Drop.
Here's a concise three-bullet summary of the attached blog post:
Introduction to Heredocs: this section introduces Heredocs, a powerful feature in various programming languages that allows you to define a block of text within the code, preserving line breaks, indentation, and other whitespace. They are particularly useful for defining multi-line strings or including large chunks of text, such as HTML or SQL queries, within the code.
This section links to a short heredoc tutprial.
Heredoc Examples in Various Languages: this final section provides examples of heredocs in multiple programming languages, including Python, R, Go, Rust, JavaScript, Bash, Ruby, Perl, PHP, and Swift.
Heredocs Are Awesome
Heredocs, short for "here documents," are a powerful feature in various programming languages that allow you to define a block of text within the code, preserving line breaks, indentation, and other whitespace. They are particularly useful for defining multi-line strings or including large chunks of text, such as HTML or SQL queries, within the code.
The syntax for writing a heredoc varies between programming languages, but in general, it involves specifying a delimiter at the beginning of the block, followed by the text to be included, and ending with the delimiter again. There are plenty of examples in section three.
Depending on the language, heredocs can contain strings, variables, commands, and other inputs, making them super versatile and contextually powerful.
In most cases, heredocs improve the readability of your code by allowing you to include large blocks of text without having to worry about escaping special characters or dealing with complicated quoting rules. They can also make your code more maintainable by reducing the need for external files or complex string concatenation. However, extensive use of heredocs in a giant script or source code file may make them hard to navigate.
Bash/Zsh HereDoc Tutorial With Examples
This is a short section, since I was going to write most of what's covered in this vendor article.
I will add one thing that isn't in there and isn't actually heredoc related.
The article shows how to use the null operator (:
) to make multiline comments in shell scripts. You can also quickly zero out and/or create a file with it via:
$ : > FILENAME
Heredocs Galore
As noted in the first section, scads of programming languages that can deal with some semblance of heredocs.
Python:
text = """
This is a
multiline string
in Python
"""
print(text)
R:
r"(This is a
multiline string
in R)" -> text
cat(text)
Go:
package main
import (
"fmt"
"github.com/MakeNowJust/heredoc"
)
func main() {
text := heredoc.Doc(`
This is a
multiline string
in Go
`)
fmt.Println(text)
}
Rust:
let text = r#"
This is a
multiline string
in Rust
"#;
println!("{}", text);
JavaScript (ES6):
const text = `
This is a
multiline string
in JavaScript
`;
console.log(text);
Bash:
cat << EOF
This is a
multiline string
in Bash
EOF
Ruby:
text = <<-EOF
This is a
multiline string
in Ruby
EOF
puts text
Perl:
my $text = <<'EOF';
This is a
multiline string
in Perl
EOF
print $text;
PHP:
$text = <<<EOT
This is a
multiline string
in PHP
EOT;
echo $text;
Swift:
let text = """
This is a
multiline string
in Swift
"""
print(text)
FIN
Unrelated to today's topic, Perplexity now has a rudimentary, official API! ☮️