hrbrmstr's Daily Drop

Share this post

Drop #347 (2023-10-05): Here[doc], Ye; Here[doc] Ye

dailyfinds.hrbrmstr.dev

Discover more from hrbrmstr's Daily Drop

A digest of all the interesting data, packages, blogs and papers covering lots of programming languages, CLI utilities, cybersecurity, data visualization, data science, web-scraping and more!
Continue reading
Sign in

Drop #347 (2023-10-05): Here[doc], Ye; Here[doc] Ye

Heredocs Are Awesome; Bash/Zsh HereDoc Tutorial With Examples; Heredocs Galore

boB Rudis
Oct 5, 2023
1
Share this post

Drop #347 (2023-10-05): Here[doc], Ye; Here[doc] Ye

dailyfinds.hrbrmstr.dev
Share

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

woman spread hand
Photo by Mohamed Nohassi on Unsplash

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.

Share

Bash/Zsh HereDoc Tutorial With Examples

love to learn pencil signage on wall near walking man
Photo by Tim Mossholder on Unsplash

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

Share

Heredocs Galore

orange and green dried leaves
Photo by Jan Huber on Unsplash

As noted in the first section, scads of programming languages that can deal with some semblance of heredocs.

  1. Python:

text = """
This is a
multiline string
in Python
"""
print(text)
  1. R:

r"(This is a
multiline string
in R)" -> text
cat(text)
  1. Go:

package main

import (
	"fmt"
	"github.com/MakeNowJust/heredoc"
)

func main() {
	text := heredoc.Doc(`
This is a
multiline string
in Go
`)
	fmt.Println(text)
}
  1. Rust:

let text = r#"
This is a
multiline string
in Rust
"#;
println!("{}", text);
  1. JavaScript (ES6):

const text = `
This is a
multiline string
in JavaScript
`;
console.log(text);
  1. Bash:

cat << EOF
This is a
multiline string
in Bash
EOF
  1. Ruby:

text = <<-EOF
This is a
multiline string
in Ruby
EOF
puts text
  1. Perl:

my $text = <<'EOF';
This is a
multiline string
in Perl
EOF
print $text;
  1. PHP:

$text = <<<EOT
This is a
multiline string
in PHP
EOT;
echo $text;
  1. Swift:

let text = """
This is a
multiline string
in Swift
"""
print(text)

Share

FIN

Unrelated to today's topic, Perplexity now has a rudimentary, official API! ☮️

hrbrmstr's Daily Drop is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.

1
Share this post

Drop #347 (2023-10-05): Here[doc], Ye; Here[doc] Ye

dailyfinds.hrbrmstr.dev
Share
Previous
Next
Comments
Top
New
Community

No posts

Ready for more?

© 2023 boB Rudis
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing