Re: Meta-perl - sorting my perl subs with perl?
by toolic (Bishop) on Jan 20, 2012 at 19:59 UTC
|
| [reply] |
|
|
Hey, that's another facepalmingly-simple solution too. Thanks!
| [reply] |
Re: Meta-perl - sorting my perl subs with perl?
by Perlbotics (Archbishop) on Jan 20, 2012 at 19:58 UTC
|
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.
| [reply] [d/l] [select] |
|
|
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 ;)
| [reply] |
|
|
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. :)
| [reply] |
|
|
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
| [reply] [d/l] |
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. | [reply] [d/l] [select] |
|
|
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
| [reply] |
|
|
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. | [reply] [d/l] |
|
|
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.
| [reply] |
|
|
|
|
|
|
|
|
|