[comp.lang.perl] here documents

maart@cs.vu.nl (Maarten Litmaath) (07/31/90)

How about adding a `<<-' here document operator, which strips leading
tabs and spaces off the lines forming the document?  (And the line
trailing the document.)  Current practice in sh.  Preserves indentation
in the script.  Currently the only workarounds I can think of are:

1)
	print 	"Newsgroups: $Newsgroups\n"
	.	"Subject: $Subject\n"
	.	"Summary: \n"
	.	"Expires: \n"
	.	"References:$References\n"
	.	"Reply-To: $Name\@$Domainname ($Fullname)\n"
	.	"Followup-To: \n"
	.	"Distribution: $Distribution\n"
	.	"Organization: $Organization\n"
	.	"Keywords: \n"
	.	"\n"
	.	"In article $Message_ID,\n"
	.	"\t$Orig_From writes:\n";

[Sic!]

2)
	sub here {
		$_[0] =~ s/((^|\n))[ \t]+/$1/g;
		$_[0] =~ s/((^|\n))\\/$1/g;
		$_[0];
	}
	print &here(<<"	EOF");
		Newsgroups: $Newsgroups
		Subject: $Subject
		Summary: 
		Expires: 
		References:$References
		Reply-To: $Name\@$Domainname ($Fullname)
		Followup-To: 
		Distribution: $Distribution
		Organization: $Organization
		Keywords: 

		In article $Message_ID,
		\\\t$Orig_From writes:
	EOF

BTW, I'd like `<<"\tEOF"' to work as well...

[Remember, we're talking about _readability_ here, Randal!  :-)  ]
--
 "and with a sudden plop it lands on usenet.  what is it? omigosh, it must[...]
   be a new user! quick kill it before it multiplies!"      (Loren J. Miller)

merlyn@iwarp.intel.com (Randal Schwartz) (07/31/90)

In article <7192@star.cs.vu.nl>, maart@cs (Maarten Litmaath) writes:
| How about adding a `<<-' here document operator, which strips leading
| tabs and spaces off the lines forming the document?  (And the line
| trailing the document.)  Current practice in sh.  Preserves indentation
| in the script.  Currently the only workarounds I can think of are:
| 
| 1)
| 	print 	"Newsgroups: $Newsgroups\n"
| 	.	"Subject: $Subject\n"
| 	.	"Summary: \n"
| 	.	"Expires: \n"
| 	.	"References:$References\n"
| 	.	"Reply-To: $Name\@$Domainname ($Fullname)\n"
| 	.	"Followup-To: \n"
| 	.	"Distribution: $Distribution\n"
| 	.	"Organization: $Organization\n"
| 	.	"Keywords: \n"
| 	.	"\n"
| 	.	"In article $Message_ID,\n"
| 	.	"\t$Orig_From writes:\n";
| 
| [Sic!]
| 
| 2)
| 	sub here {
| 		$_[0] =~ s/((^|\n))[ \t]+/$1/g;
| 		$_[0] =~ s/((^|\n))\\/$1/g;
| 		$_[0];
| 	}
| 	print &here(<<"	EOF");
| 		Newsgroups: $Newsgroups
| 		Subject: $Subject
| 		Summary: 
| 		Expires: 
| 		References:$References
| 		Reply-To: $Name\@$Domainname ($Fullname)
| 		Followup-To: 
| 		Distribution: $Distribution
| 		Organization: $Organization
| 		Keywords: 
| 
| 		In article $Message_ID,
| 		\\\t$Orig_From writes:
| 	EOF
| 
| BTW, I'd like `<<"\tEOF"' to work as well...
| 
| [Remember, we're talking about _readability_ here, Randal!  :-)  ]

Well, since you invoked the name of the daemon, here's what I do:

==================================================

## next few lines are magic to get reasonable output.
## pay no attention to the man behind the curtain...
for (split(/\s+/, <<EOF)) {
Newsgroups Subject References Name Domainname Fullname
Distribution Organization Message_ID Orig_From
EOF
	$old = $_; y/a-zA-Z/A-Za-z/; eval qq#\$$old = "$_";#;
}

$somestring = join("\n", grep(s/^\t//,split(/\n/, <<"EOF")));
	Newsgroups: $Newsgroups
	Subject: $Subject
	Summary: 
	Expires: 
	References:$References
	Reply-To: $Name\@$Domainname ($Fullname)
	Followup-To: 
	Distribution: $Distribution
	Organization: $Organization
	Keywords: 

	In article $Message_ID,
	\t$Orig_From writes:
EOF

print "$somestring";

==================================================

I don't consider that to be *too* unreadable.  I mean, it reads
from left to right, if you can parse parens. :-)

Now, *here's* unreadable:

vec($x,0,8)++; $x x= 25; print $x ^ "Ktru!`onuids!Qdsm!i`bjds-"
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

maart@cs.vu.nl (Maarten Litmaath) (08/01/90)

In article <1990Jul31.013220.3903@iwarp.intel.com>,
	merlyn@iwarp.intel.com (Randal Schwartz) writes:
)In article <7192@star.cs.vu.nl>, maart@cs (Maarten Litmaath) writes:
					^^
					Something needs fixing, Randal.

You provided another _workaround_, Randal.  Furthermore just like my
`&here' solution it involved _run-time_ processing of the here-document,
whereas IMHO the final string (modulo variable substitutions etc.) should
be found at _compile-time_.
Therefore my request still stands:

)| How about adding a `<<-' here document operator, which strips leading
)| tabs and spaces off the lines forming the document?  (And the line
)| trailing the document.)  Current practice in sh.  Preserves indentation
)| in the script.  [...]
)...
)| 	print &here(<<"	EOF");

Now why doesn't the following work as intended?

	print &here(<<"\tEOF");
--
 "and with a sudden plop it lands on usenet.  what is it? omigosh, it must[...]
   be a new user! quick kill it before it multiplies!"      (Loren J. Miller)