Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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, then running
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://536730]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-02-25 08:38 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.