Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Just another Perl shrine
 
PerlMonks  

Re: getting count of method params

by unobe (Scribe)
on Mar 14, 2006 at 23:27 UTC ( #536730=note: print w/ replies, xml ) Need Help??


in reply to getting count of method params

My java is a little rusty, but IIRC a method always has an optional access modifier at the start, then the return type, and then the method name (followed by the parameters). A one liner that might help is (very ugly):

grep { print scalar (@params = /^\s*(?:(?:public|private|protected)\s+ +)?\w+\s+\w+\s*\(([^,]+)(\,[^,]+)*\)/g);} <>;
If you save the above code as test.pl, then running
perl test.pl FILENAME
will give you the number of parameters for each method. The diamond operator will automatically fetch the contents of whatever file you pass to the script at the command line (or STDIN, if no file supplied). The above code, made easier to read:
grep { print scalar # print the number of items matched (@params = /^ # any whitespace at start of line \s* #optional access modifier (?:(?:public|private|protected)\s+) # return type and method name \w+\s+\w+\s* \( # params separated by commas ([^,]+)(\,[^,]+)* \) # don't really need m or s # since only one line is read at a time, # but I find it a good habit to always # use them /gxms); } <>; # read from keyboard or the file(s) provided on command line
I second izut's suggestion: read the perldocs.


Comment on Re: getting count of method params
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://536730]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-04-20 19:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (486 votes), past polls