To do this properly is difficult. As a start you need to do somthing like this:
local $/; # undef $/ so we glob data in
$data = <DATA>; # get all our data as a string
$data =~ s|//.*\n|\n|g; # strip single line comments
$data =~ s|/\*.*?\*/||gs; # strip multi-line comments
$data =~ s|^\s*\n||gm; # strip blank lines
$data =~ s|^\s+||gm; # strip leading whitespace
$data =~ s|\s+$||gm; # strip trailing whitespace
# do the magic and split into functions
@functions = split /(?=\bfunction\s)/, $data;
# pudding proof
print "$_ ---\n" for @functions;
__DATA__
var answer = 42; // just because
function 1 {
blah // comment;
}
/* this function is no good
function 2 {
blah
} */
function 3 {
if (functionvar){ // var name not function keyword
// do the blah stuff
}
}
/* this is
another comment
*/
function 4 {
just another perl hacker
}
You need to remove all the comments first to ensure that
the only occurences of the word function represent keywords.
You can then just use split with a positive lookahead assertion
in the regex to generate your array. The next step you want to
do is much harder but that is another story.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|