Just to see if this made things more readable, I substituted all the positional captures with named captures. See Regexp::NamedCaptures, at last for the module code.

use Regexp::NamedCaptures; @files = glob "*.xml"; undef $/; for $file (@files) { $indent = 0; open FILE, $file or die "Couldn't open $file for reading: $!"; $_ = readline *FILE; close FILE or die "Couldn't close $file: $!"; # Remove whitespace between > and < if that is the only thing sepa +rating # them s/(?<=>)\s+(?=<)//g; # Indent s{ # Capture a tag <$close_tag$name$empty_tag>, # a potential closing slash $close_tag # the contents $name # a potential closing slash $empty_tag <(?<\$close_tag>/?)(?<\$name>[^/>]+)(?<\$empty_tag>/?)> # Optional white space \s* # Optional tag. # $4 contains either undef, "<" or "</" (?=(?<\$next_tag_start></?))? } { # Adjust the indentation level. # $3: A <foo/> tag. No alteration to indentation. # $1: A closing </foo> tag. Drop one indentation level # else: An opening <foo> tag. Increase one indentation level $indent += $empty_tag ? 0 : $close_tag ? -1 : 1; # Put the captured tag back into place "<$close_tag$name$empty_tag>" . # Two closing tags in a row. Add a newline and indent the next + line ($close_tag and ($next_tag_start eq "</") ? "\n" . (" " x $indent) : # This isn't a closing tag but the next tag is. Add a newline +and # indent the next line. $next_tag_start ? "\n" . (" " x $indent) : # This isn't a closing tag - no special indentation. I forget +why # this works. "" ) # /g repeat as necessary # /e Execute the block of perl code to create replacement text # /x Allow whitespace and comments in the regex }gex; open FILE, ">", $file or die "Couldn't open $file for writing: $!" +; print FILE or die "Couldn't write to $file: $!"; close FILE or die "Couldn't close $file: $!"; }

In reply to Re: Light batch XML indenter ( rewritten with Regexp::NamedCaptures ) by diotalevi
in thread Light batch XML indenter by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.