barryscott has asked for the wisdom of the Perl Monks concerning the following question:
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...int test_function (int a, char b) { /* some code here*/ }
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.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 = $_; } } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: help with perl understanding
by svenXY (Deacon) on Jan 08, 2008 at 09:40 UTC | |
Re: help with perl understanding
by locked_user sundialsvc4 (Abbot) on Jan 08, 2008 at 16:37 UTC | |
by graq (Curate) on Jan 09, 2008 at 08:48 UTC | |
Re: help with perl understanding
by Anonymous Monk on Jan 08, 2008 at 16:28 UTC |