split a big program into multiple files

X

xhoster

Hi,

my program is getting big and I'd like to split it into multiple files,
something like C guys do.

Why? Is it so big that it is crashing your favorite text editor?
main.pl

include file1.pl
include file2.pl

etc

I tried both do and require but they doesn't work as I expect:

I can't use the global variables I declare into main for the subrutines
declared into other files.

I assume that by "global" you mean file-level lexicals.

Generally, you are supposed to pass arguments into subroutines, not pick
them up randomly from the symbol table/pad.
How can I do?

If you don't want to experience the benefits of using file-level lexicals,
then don't use them. Use package variables instead (or much better,
redesign your program into multiple files along lines such that you don't
need variables to cross the boundaries.)

Xho
 
F

filippo2991

Hi,

my program is getting big and I'd like to split it into multiple files,
something like C guys do.

main.pl

include file1.pl
include file2.pl

etc

I tried both do and require but they doesn't work as I expect:

I can't use the global variables I declare into main for the subrutines
declared into other files. How can I do?

Thanks,

Filippo
 
J

John Bokma

Hi,

my program is getting big and I'd like to split it into multiple files,
something like C guys do.

main.pl

include file1.pl
include file2.pl

etc

I tried both do and require but they doesn't work as I expect:

What did you expect?
I can't use the global variables I declare into main for the subrutines
declared into other files. How can I do?

Best option would be to group functionality into Perl modules. Instead of
global variables you might want to create objects and initialize them with
those "global" settings.

Might sound as a lot of extra work, but in the end you have a piece of
work that is much easier to maintain.

Also, while grouping functionality, you might keep a close eye on CPAN.
Maybe someone has already written a module that can replace a part of your
code and it might be better to use the CPAN one, especially if it's a
module that is used by many.
 
F

filippo

ok John and Xho, you convinced me :)

In fact I have just a couple of global variables so I can easily
redesign my subroutines to manage these as arguments.

Which is the best method to have a C-like splitting method?

Thanks for your help.

Filippo
 
J

Josef Moellers

filippo said:
ok John and Xho, you convinced me :)

In fact I have just a couple of global variables so I can easily
redesign my subroutines to manage these as arguments.

Which is the best method to have a C-like splitting method?

What on earth is a "C-like splitting method"?
In C (and in Perl, and in Modula2, and in any other language that allows
to spread source code over more than a single source file), you split by
grouping things that belong together.

When using #include, you put only definitions (types, structures,
function prototypes, constants) into the files that are included.
Otherwise you put code that belongs together into a C source file and
compile each source file independently into object files which are then
linked together into an executable. The .h-files included provide some
glue between these source files.
 
F

filippo

Josef Moellers ha scritto:

ok, you're right.

My application is a Tk GUI based perl program to manage a touristic
campsite. I have some windows:

Main Menu
createGroup
manageGroup
Bill
others (tools)

and I want to split such a big code (~10.000rows) into separate files.
I have no global variables (no more), just subroutines: more or less a
main subroutine plus a couple of service for each windows. Which is the
'best' method to split my program?

Thanks,

Filippo
 
J

Josef Moellers

filippo said:
Josef Moellers ha scritto:

ok, you're right.

My application is a Tk GUI based perl program to manage a touristic
campsite. I have some windows:

Main Menu
createGroup
manageGroup
Bill
others (tools)

and I want to split such a big code (~10.000rows) into separate files.
I have no global variables (no more), just subroutines: more or less a
main subroutine plus a couple of service for each windows. Which is the
'best' method to split my program?

It's indeed probably the most complicated task: to decide which
functions should be grouped together.
I'm not sure if there is a clean-cut approach to do this.

Maybe you could group according to "window"?

Josef
 
F

filippo

I know more or less how to split. I'd like to know the best way to
import. I know the 'do' way, are there problems to do this using do?

Thanks,

Filippo
 
J

Josef Moellers

filippo said:
I know more or less how to split. I'd like to know the best way to
import. I know the 'do' way, are there problems to do this using do?

Oh, sorry for the misunderstanding.

As for the technicality: I really don't know what's the best way to do.
I usually use "use" which is based on "require" which is based on "do".

Hopefully others know better.

Josef
 
B

Ben Morrow

Quoth "filippo said:
I know more or less how to split. I'd like to know the best way to
import. I know the 'do' way, are there problems to do this using do?

The modern way is to use 'use'. Read perlmod and perlnewmod.

Ben
 
S

Sherm Pendley

Why? Is it so big that it is crashing your favorite text editor?

I haven't seen a file so big it crashed my favorite editor.

I *have* seen files so big they nearly crashed my favorite brain. :)

sherm--
 
X

xhoster

Sherm Pendley said:
I haven't seen a file so big it crashed my favorite editor.

I haven't seen the code portion of a Perl file that was that big. Other
file types, or __DATA__ sections, sure.
I *have* seen files so big they nearly crashed my favorite brain. :)

If you had just broken it into smaller chunks at random
(say, by using the gnu split command), would that have helped your brain?

If not, then you don't have a technical problem about how to get perl to
stick random chunks back together, but rather an intellectual one about how
to take things apart.

I think we are getting at the same thing, just with different levels of
sarcasm :)


Xho
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top