Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Meta-perl - sorting my perl subs with perl?

by Syndaryl (Initiate)
on Jan 20, 2012 at 19:46 UTC ( #949032=perlquestion: print w/replies, xml ) Need Help??
Syndaryl has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a perl project that's very rapidly spawning subroutines, even in the middle of development (the client keeps widening the specifications...) I've got 40 subs in my module already and I'm expecting the final count to be 150+ before the client gets bored with making my life interesting.

How can I sort this big pile of subs alphabetically? I'm not allowed to whip the client until they're organized, but I can whip my module into shape...

I think basically I need a regex to recognize a perl sub - but that's a real bear with the brace balancing issue.

  • Comment on Meta-perl - sorting my perl subs with perl?

Replies are listed 'Best First'.
Re: Meta-perl - sorting my perl subs with perl?
by toolic (Bishop) on Jan 20, 2012 at 19:59 UTC
      Hey, that's another facepalmingly-simple solution too. Thanks!
Re: Meta-perl - sorting my perl subs with perl?
by Perlbotics (Chancellor) on Jan 20, 2012 at 19:58 UTC

    Maybe you can start with B::Xref and render the output as a report?

    perl -MO=Xref YourMod.pm

    Update: Perhaps, I misunderstood... you seem to want to sort the source code, such that e.g. sub a_func comes before/above sub b_func?

    Personally, I would rather group the subs by functionality and let the editor do the work localising the subs (e.g. Emacs Imenu, other editors have similar functionality).

    But with an amount of 150 subs, there is a good chance they can be grouped in specialised (sub-)modules, reducing the set of subs to an amount that can be managed (read: edit, document, test) with less effort.

      Personally, I would rather group the subs by functionality and let the editor do the work localising the subs (e.g. Emacs Imenu, other editors have similar functionality).
      +1 Or if it has to be alphabetical, what about just inserting them in this order? It's not like you write a new sub every couple of seconds.
      But with an amount of 150 subs, there is a good chance they can be grouped in specialised (sub-)modules, reducing the set of subs to an amount that can be managed (read: edit, document, test) with less effort.
      Double plus good! Plus you get the alphabetical sorting as ls' default ;)

      I'm already grouping subs by functionality. This is literally a cluster of 150 subs with related functionality - the rest of the program is elsewhere :/

      To expand a little on how this particular project is merrily spiraling out of control:

      Project is text extraction from a print-ready format never meant to be used as a text source, parsing the natural-language text out of known tables to XML, and finally burping up reformatted text via FO.

      Client initially indicated they didn't have that many uniquely-presenting tables to rip out. Client provided samples (in PDF, not the production format) for our assessment that were consistent with this claim. We received samples in the production format and found a rather hairier situation. But nobody Upstairs is changing deadlines :/ Since the client apparently can't count, the "solution" is that they're now regularly sending us more samples, where we find more types of tables and have to shove more items onto the match-text-goto-handling-sub hash, even as we're trying to do everything else.

      The team IDing new sections and match regexes are batch-generating skeletons of the relevant parts of the code via a couple of algorithms, which I then get to put into my module and flesh out. The last thing anyone's really been concerned about is alphabetizing the "handle text in table format XYZ" subs. I don't think I can justify to my boss taking half an hour or so out to reorganize the ones we have just because it annoys me, and frankly that's more boring than I find it irritating. If I'm going to do it on my own time, I'd rather write script than cut and paste code. :)

        stick __END__ at the top of your file, then use

        perl -MAutoSplit -e  " autosplit( $ARGV[0], $ARGV[1], 0, 0 ) " lib/Some/File.pm  out/dir/like/a/lib

        This will generates one file for each function, then you can glob/sort/cat and reconstitute Some::File with subs in sorted order

Re: Meta-perl - sorting my perl subs with perl?
by JavaFan (Canon) on Jan 20, 2012 at 21:18 UTC
    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.

      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

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://949032]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2018-12-18 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (80 votes). Check out past polls.

    Notices?