Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: getting count of method params

by unobe (Scribe)
on Mar 14, 2006 at 23:27 UTC ( [id://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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found