Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Meta-perl - sorting my perl subs with perl?

by JavaFan (Canon)
on Jan 20, 2012 at 21:18 UTC ( [id://949054]=note: print w/replies, xml ) Need Help??


in reply to Meta-perl - sorting my perl subs with perl?

I don't know about your coding style, but I can always find all my subs without a single false positive by searching for /^sub /. And searching for the next /^} will find the closing brace.

Update: I also have single line subs where the closing brace will be on the same line as the sub keyword. But that's trivially to test for. /Update

I really hope I never have to maintain any code for which that would not be true.

Replies are listed 'Best First'.
Re^2: Meta-perl - sorting my perl subs with perl?
by CountZero (Bishop) on Jan 20, 2012 at 21:56 UTC
    And if your coding style is less than perfectly standard, PerlTidy is your friend!

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re^2: Meta-perl - sorting my perl subs with perl?
by educated_foo (Vicar) on Jan 20, 2012 at 22:55 UTC
    This should work. But think hard about keeping comments and POD in the right place, e.g. either at the top of the file or before the sub to be commented. You might start with a 4-state state machine:
    start-of-file -> ??? -> sub-header sub-header -> /^sub / -> in-sub in-sub -> /^}$/ -> sub-header sub-header -> eof() || /^__(END|DATA)__$/ -> end-of-file
    And remember to save that last non-empty "sub-header" when you reach EOF.
      In my world, there's only place for POD to go: at the end of the file.

      Comments are easy. If there are comments related to a sub, but outside of the sub, they'll precede the sub and have their comment marker in the left column.

      But luckily, I never have felt the urge to sort my subs in alphabetical order; to me that makes as much sense as sorting my bookshelves on colour. I group related subs together.

        In my world, there's only place for POD to go: at the end of the file.
        After getting used to Emacs's docstrings, I find that style annoying. The docstring is actually within the function, explains its usage, and gets extracted when you compile documentation. If you put all your POD at the end, you either lose the ability to put usage next to the function, or have to duplicate your POD in a comment.

        As I mentioned upstream, it's difficult to subgroup this... morass more. I've already pulled out the content that's going to be on every document into another module from the stuff that's optional and might show up "anywhere" into another module.

        The naming scheme on the subs is already going to group subs sorted alphabetically about as well as I can get them further grouped.

        It's either that or I'll have three to five modules with two to four subs in them (for the few tables that have a few variations), and a gigantic "misc" lump. Which is barely helpful.

        But thank you, /^sub/ is a facepalmingly simple solution. Of course I don't need to balance braces. Jeeze. All this text is rotting my brain.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-26 08:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found