Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

getting count of method params

by neeha (Novice)
on Mar 14, 2006 at 11:43 UTC ( [id://536527]=perlquestion: print w/replies, xml ) Need Help??

neeha has asked for the wisdom of the Perl Monks concerning the following question:

Hi Experts,
I am reading a java program through perl.
The code has something like
public void check1(String str,String str1)
Is there any way by which i can get the count of params of the "check1" method using perl.

Replies are listed 'Best First'.
Re: getting count of method params
by izut (Chaplain) on Mar 14, 2006 at 12:21 UTC

    You can read perlre and perlretut. I would start with something like this, assuming all methods definition will be in one line:

    while (<DATA>) { if (/^ # Matches line start \s* # None or many spaces (?:public|private)? # Matches 'public', 'private' or none (pac +kages) \s+ # One or many spaces (?:\S+) # Matches one or more non spacing chars (v +ariable type) \s+ # One or many spaces (?:\S+) # Matches one or more non spacing chars (m +ethod name) \s* # None or many spaces \( # Literal '(' (.*) # Matches everything \) # Literal ')' \s* # None or many spaces $ # Matches end of line /x) { print $1, "\n"; } } __DATA__ public void check1(String str, Integer i) { }

    Igor 'izut' Sutton
    your code, your rules.

Re: getting count of method params
by rafl (Friar) on Mar 14, 2006 at 11:50 UTC

    Parameters to Java methods are usually enclosed between brackets and seperated by commas, right?

    So how about extracting the part between the brackets and spliting that part at the comma? The number of elements in the list returned by split should tell you how many arguments a given method could take.

    Cheers, Flo

      I was doing it that way only.But my code looks quite lengthy.Is there any regular expressions by which i can achieve it.I am not that experienced in Perl so wanted to know if we can achive it in a short way.
Re: getting count of method params
by unobe (Scribe) on Mar 14, 2006 at 23:27 UTC
    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: perlquestion [id://536527]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-20 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found