Perlembed: split code snippet, normalize indentation#24573
Conversation
This improves readability with syntax highlighters
|
Seems like consistent 4 spaces to start a code block is not feasible because of the 78 character limit here. So I reverted that to 1 space for big code blocks, while others consistently use 4 spaces. Also slightly reorganized |
leonerd
left a comment
There was a problem hiding this comment.
Overall looking a good improvement. But while you're here editing it anyway, there's a few more edits that would be nice. Commented inline.
| local *FH; | ||
| open FH, $filename or die "open '$filename' $!"; | ||
| local($/) = undef; | ||
| my $sub = <FH>; | ||
| close FH; |
There was a problem hiding this comment.
If you're going for making more edits than just strictly whitespace, I'd like to see this updated to not use global filehandles or 2-arg open.
As a first cut, maybe go for 3-arg open into a lexical:
open my $fh, ">", $filename or die ...Though maybe consider if we want to use File::Slurp::Tiny or somesuch to simplify further.
| my $sub = <FH>; | ||
| close FH; | ||
|
|
||
| #wrap the code into a subroutine inside our unique package |
There was a problem hiding this comment.
Common style on other lines is to put a space after the # character:
# wrap the code ...
| my $eval = qq{package $package; sub handler { $sub; }}; | ||
| { | ||
| # hide our variables within this block | ||
| my($filename,$mtime,$package,$sub); |
There was a problem hiding this comment.
Also some whitespace here:
my ($filename, $mtime, $package, $sub);|
Right, I did not want to get my PR too large in case it gets rejected. If you want me to freshen up the code examples a bit here as well, I can do that. |
While reading perlembed docs, I noticed the snippet under https://perldoc.perl.org/perlembed#Maintaining-a-persistent-interpreter is not very readable - this is because one snippet mixes both Perl and C, and C part (the more important one in this document) gets syntax-colored according to Perl syntax rules, which makes it very unpleasant to read.
This PR splits that code snippet into two so that this should no longer be the case. While at it, I also noticed the indentation of code snippets is all over the place, with even occasional tab characters mixed in. I normalized that to be 4 spaces deep, and each code block is also started by 4-space indent, which hopefully helps plaintext readability.
While changing indentation I had to re-justify a few parts in the code, so I took the liberty to do it slightly differently than it was before, though the differences should be minimal.