Splitting subroutines out of a file

J

Jos De Laender

Dear group ,

I'm in a hurry to split out a number of subroutines in a bunch of pl files.
(getting them in separate files)

I realize it is not rocket science , but getting everything right taking
into account curly braces that may appear in the subroutine may lead me to
a mini parsing routine.

Somebody knows about available routines or modules doing this ?

The autosplit comes near , but it works only on more or less correctly
formed modules , which is not my case (ordinary perl files)

Thanks for any suggestions or help.
 
J

James E Keenan

"Jos De Laender"
I'm in a hurry to split out a number of subroutines in a bunch of pl files.
(getting them in separate files)

I realize it is not rocket science , but getting everything right taking
into account curly braces that may appear in the subroutine may lead me to
a mini parsing routine.
Why? Either these subroutines are currently operating correctly in your
main package (I hope that you are using both warnings and strict) -- in
which case they should work properly when imported -- or they are not.
Somebody knows about available routines or modules doing this ?

The autosplit comes near , but it works only on more or less correctly
formed modules , which is not my case (ordinary perl files)
In Perl 5 (i.e., for the past 9 years), the standard, recommended way to
import subroutines into a main package has been to place them in a module
ending in a ".pm" extension and to use Exporter to designate which
subroutines are always inported into main, which are imported on request,
etc. See 'perldoc perlmod'. What is your rationale for planning to use a
deprecated procedure?
 
J

Jos De Laender

James said:
<big snip pointing to a misunderstanding>

Probably I didn't explain well enough.
I inherited a bunch of .pl files. Probably containing a number of times the
same subroutine into the pl file.
Before cleaning up I want to "copy out" all those subroutines in order to
do some comparing on them based on diff.
In a next step I collect them in a module where they can be maintained then
consistently.
The help I'm looking for is for the first step.
 
B

Bob Walton

Jos said:
James E Keenan wrote:



Probably I didn't explain well enough.
I inherited a bunch of .pl files. Probably containing a number of times the
same subroutine into the pl file.
Before cleaning up I want to "copy out" all those subroutines in order to
do some comparing on them based on diff.
In a next step I collect them in a module where they can be maintained then
consistently.
The help I'm looking for is for the first step.

So if I understand you correctly, you want to parse your .pl files to
separate out individual sub's into individual files. So you need a
parser that will parse Perl source code. I'm not aware of any, but you
might try looking on CPAN. The "B" series of modules might have
something, but it doesn't look like it at first glance. B::Xref looks
like its output could be used to generate a list of sub names and line
numbers without much trouble, which could perhaps then be used to do the
job.

My approach would be to manually examine the source in an editor and
insert a special flag between subs (something like a line consisting of:

#~~sub~~

-- anything that won't otherwise appear in the code. Then process the
file with that as the input record separator. You will have to decide
how to name the files, since there might be duplicate sub names between
files and perhaps between packages in one file (and perhaps anonymous
subs too?) -- use a sequence number?

But all in all, I don't grok what doing all that would buy you over
simply manipulating the code into the files you want using an editor --
it looks like that's what you're going to end up doing anyway.
 
J

James E Keenan

Bob Walton said:
Jos De Laender wrote:


My approach would be to manually examine the source in an editor and
insert a special flag between subs (something like a line consisting of:

#~~sub~~

-- anything that won't otherwise appear in the code. Then process the
file with that as the input record separator. You will have to decide
how to name the files, since there might be duplicate sub names between
files and perhaps between packages in one file (and perhaps anonymous
subs too?) -- use a sequence number?

But all in all, I don't grok what doing all that would buy you over
simply manipulating the code into the files you want using an editor --
it looks like that's what you're going to end up doing anyway.

--
Jos: I think Bob is right on the money here.
 
J

Jos De Laender

James said:
Jos: I think Bob is right on the money here.

Thanks to you all.

I used Bob's approach for identifying and copying out the subs.

You wont't believe, but the cgi application worked with some 20 files
(called by different buttons in the webpage) in which 8 subroutines where
more or less the same. In file1 it was patched in file2 it wasn't etc ...
That's why I wanted to reorganise drastically and not simply manipulating
code into the files ....

Once more thanks.

Jos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top