Is there an include for perl?

B

Bill H

I am looking for an include for perl. I know about require, use etc
and use them, but I have a program that is getting large and would
like to put some of the code into their own files and just use a
include "filename" where the code I pulled belongs and have nothing
else done to that included code other than it being inserted when the
program is run. As an example:


main program:

line 1
line 2
include "other program"
line 3
line 4

other program
line 2a
line 2b
line 2c

when perl runs main program it sees it as

line 1
line 2
line 2a
line 2b
line 2c
line 3
line 4

I know you can do this with require but I have issues with variables
being set in one require not being available in another

Bill H
 
A

A. Sinan Unur

I am looking for an include for perl. I know about require, use etc
and use them, but I have a program that is getting large and would
like to put some of the code into their own files and just use a
include "filename" where the code I pulled belongs and have nothing
else done to that included code other than it being inserted when the
program is run. As an example:

http://search.cpan.org/~autrijus/Filter-Macro-0.11/lib/Filter/Macro.pm

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
J

Jürgen Exner

Bill H said:
I am looking for an include for perl. I know about require, use etc
and use them, but I have a program that is getting large and would
like to put some of the code into their own files and just use a
include "filename" where the code I pulled belongs and have nothing
else done to that included code other than it being inserted when the
program is run.

Are you looking for do()?

jue
 
M

Mark Clements

Bill said:
I am looking for an include for perl. I know about require, use etc
and use them, but I have a program that is getting large and would
like to put some of the code into their own files and just use a
include "filename" where the code I pulled belongs and have nothing
else done to that included code other than it being inserted when the
program is run. As an example:


main program:

line 1
line 2
include "other program"
line 3
line 4

other program
line 2a
line 2b
line 2c

when perl runs main program it sees it as

line 1
line 2
line 2a
line 2b
line 2c
line 3
line 4

I know you can do this with require but I have issues with variables
being set in one require not being available in another
You may want to consider exactly what you want to accomplish by this.
Breaking your program up into separate chunks to improve manageability
is no bad thing (quite the opposite), but the normal mechanism is
creating modules and "use"ing them. Simply doing a textual include as
you want to do may seem to be the simplest solution technically, but may
not be particular good design. Lines 2a-2c in the example above could be
replaced by a simple call to a subroutine in an external module.

Mark
 
J

Jürgen Exner

Bill H said:
Hi Jurgen. I looked at do() and it didn't seem to fit what I want.

Well, I suppose you can always run your Perl program through cpp first
and feed the result to perl.

jue
 
N

nolo contendere

Hi Jurgen. I looked at do() and it didn't seem to fit what I want.

How does do() not fit what you want? It seems to exactly fit your
requirements to me...
 
B

Ben Morrow

Quoth Bill H said:
I am looking for an include for perl. I know about require, use etc
and use them, but I have a program that is getting large and would
like to put some of the code into their own files and just use a
include "filename" where the code I pulled belongs and have nothing
else done to that included code other than it being inserted when the
program is run. As an example:


main program:

line 1
line 2
include "other program"
line 3
line 4

other program
line 2a
line 2b
line 2c

when perl runs main program it sees it as

line 1
line 2
line 2a
line 2b
line 2c
line 3
line 4

I know you can do this with require but I have issues with variables
being set in one require not being available in another

Use require and declare all variables that you want to be shared between
files with 'our' instead of 'my'.

Generally speaking this is a bad idea. It is much better to split the
code into logical sections, with well-defined means of communication
between them. Then you put each section into a proper module.

Ben
 
P

Peter J. Holzer

More likely for eval.

How does do() not fit what you want?

Probably in exactly the same way as require and use don't fit, as the
visibility of variables is the same in all three cases.

But that's a feature, not a bug. If a program is getting too large, you
need to separate it so that you can understand the parts individually
(the times when you had to split your files because otherwise the editor
or compiler would choke on them are hopefully long gone). Otherwise it
becomes harder to understand, not easier.

hp
 
H

Hans Mulder

Jürgen Exner said:
Well, I suppose you can always run your Perl program through cpp first
and feed the result to perl.

If you want to use the cpp, consider perl's -P option, which does
exactly that.

Incidentally, the section on -P in perlrun explains why this is a
bad idea.

Hope this helps,

-- HansM
 
N

nolo contendere

More likely for eval.



Probably in exactly the same way as require and use don't fit, as the
visibility of variables is the same in all three cases.

Ahh, I missed the OP's mention of variable scope. Ben's suggestion
would solve that issue.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top