As far as dealing with extracting code, both of your routines are broken; neither deal with end-curlies in quotes. For instance, both routines will bomb on:

sub blah { return '}'; }

You can use Text::Balanced::extract_codeblock($code, '{}'); instead of

extract_bracketed($code, '{}') to handle this, but even that will stil +l bomb on curlies in comments: </p> <code> sub blah { # } is such a neat character return '}'; }

If you want to handle this case, but don't want the complexity of writing a real parser, then you can try this hack instead:

$code = blockize($code); sub blockize { my ($code) = @_; # describe a block: use re 'eval'; my $comment = qr/#[^\n]*/; my $single_quoted = qr/' [^\\']+ (?: \\. [^\\']+ )* '/; my $double_quoted = qr/" [^\\"]+ (?: \\. [^\\"]+ )* "/; my $block; $block = qr/ { (?: (?> [^#'"{}]+ ) | $comment | $single_quoted | $double_quoted | (??{$block}) )* } /x; # now that we have a block definition set up, its just a simple su +bstitution $code =~ s/sub \s+ (\w+) \s+ ($block)/ format_code("$1", "$2") /ge +x; return $code; sub format_code { my ($name, $block) = @_; $block =~ s/\n/\n /g; return "{\n sub $name $block\n}"; } }

It will even format the added block nicely :) Hopefully your perl-like language doesn't have pod; that's a whole 'nother beast.

We could even clean that up a little bit, by using Regex::Common:

sub blockize { my ($code) = @_; # describe a block: use re 'eval'; use Regex::Common qw(delimited comment); my $block; $block = qr/ { (?: (?> [^#'"{}]+ ) | $RE{comment}{Perl} | $RE{delimited}{-delim=>"'"} | $RE{delimited}{-delim=>'"'} | (??{$block}) )* } /x; # now that we have a block definition set up, its just a simple su +bstitution $code =~ s/sub \s+ (\w+) \s+ ($block)/ format_code("$1", "$2") /ge +x; return $code; sub format_code { my ($name, $block) = @_; $block =~ s/\n/\n /g; return "{\n sub $name $block\n}"; } }

In reply to Re: parsing code, finding block boundaries by jryan
in thread parsing code, finding block boundaries by revdiablo

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.