Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Converting to sub signatures

by cavac (Parson)
on Jun 23, 2022 at 18:33 UTC ( [id://11144997]=note: print w/replies, xml ) Need Help??


in reply to Re: Converting to sub signatures
in thread Converting to sub signatures

You are absolutely right about the regular expressions not working all that well. It did in fact fumble some commented out functions into non-working code.

I very seldomly use negative matches ("must not contain" stuff), usually i check first if the line is a comment, and THEN decide if a want to process further. How would i correctly anchor this?

Example code lines:

should match these: sub hello { sub hello { should not match these: #sub hello { # sub hello { # sub hello { # sub hello {

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

Replies are listed 'Best First'.
Re^3: Converting to sub signatures
by kcott (Archbishop) on Jun 23, 2022 at 19:17 UTC

    G'day cavac,

    This does/doesn't match as per your example:

    qr{^\s*[^#]*\s*sub}

    Quick test:

    #!/usr/bin/env perl use v5.26; my $re = qr{^\s*[^#]*\s*sub}; my (@match, @no_match); while (<DATA>) { if (/$re/) { push @match, $_; } else { push @no_match, $_; } } say '*** Matched:'; print for @match; say '*** Not Matched:'; print for @no_match; __DATA__ should match these: sub hello { sub hello { should not match these: #sub hello { # sub hello { # sub hello { # sub hello {

    Output:

    *** Matched: sub hello { sub hello { *** Not Matched: should match these: should not match these: #sub hello { # sub hello { # sub hello { # sub hello {

    — Ken

Re^3: Converting to sub signatures
by hv (Prior) on Jun 23, 2022 at 19:38 UTC

    I guess the next question is: are there any examples you want to match that do not start m{ ^ \s* sub \b }x? If not, then you have your answer; if there are, those are the tricky cases we'd need to see.

    I just took a quick look at one of my codebases and found 558 matches for that pattern; 503 of them were normal sub declarations all matching m{ ^ \s* sub \s+ (\w+) \s* \{ }x, the rest were anonymous sub references not matching that pattern. Of matches against m{ \b sub \b }x elsewhere in any line, almost all were anonymous sub references (the remaining 2 or 3 were comments or $sub variables).

    So that pattern would work for me, would it work for you?

Log In?
Username:
Password:

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

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

    No recent polls found