Good morning Monks. I'm struggling to extract functions from c code,
int test_function (int a, char b) { /* some code here*/ }
and was very kindly helped out by the forum with the following code snippet, which (with my limited but fast growing knowledge of perl) looks like the kind of thing I'm after...
my ($prev, $type, $func, $args, $body); while (<>) { if (/^\{/) { # maybe check for structs/classes here if necessary ($type, $func, $args) = split(' ', $prev, 3); $body = ''; } elsif (/^\}/) { # ... process the function here ... $func = undef; } else { if (defined($func)) { $body .= $_; } else { $prev = $_; } } }
but I have some questions to understand it fully. Firstly - is $prev a special variable? How does perl know to use the line previous? Is something clever happening on the regex that I can't identify? Next, do I require a file to be looked at line by line, or "slurped" in? Can someone very quickly let me know, and the advantages of each? Help is *much* appreciated, I'm trying to design some tools for analysing my source code and learning perl at the same time. ---EDIT--- I've had some coffee, steamed open my eyes and sat down with a manual. I can now see how it works - sorry to bother ;oD This language really is amazing you know, so versatile, but if you don't fully understand some subtle features it can be very difficult to read. I'm an embedded c programmer by trade, so this high level work amazes me.

In reply to help with perl understanding by barryscott

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.