how to put subroutines in separate files and call?

how to put subroutines in separate files and call?

am 18.01.2008 18:44:14 von birdsong9

I'm a novice perl programmer but have been thinking about this
question for a long time and finally decided to see if I could get
some advice... I have a program that is several thousand lines of
code, mostly broken up into dozens of subroutines, and if it's
possible to do so would like to be able to call a subroutine that is
stored in a separate file location and have it share data back and
forth with my program. Is it possible to do this without creating
modules?

Any assistance would be greatly appreciated.

Re: how to put subroutines in separate files and call?

am 18.01.2008 18:56:20 von Joost Diepenmaat

birdsong9@gmail.com writes:

> I'm a novice perl programmer but have been thinking about this
> question for a long time and finally decided to see if I could get
> some advice... I have a program that is several thousand lines of
> code, mostly broken up into dozens of subroutines, and if it's
> possible to do so would like to be able to call a subroutine that is
> stored in a separate file location and have it share data back and
> forth with my program. Is it possible to do this without creating
> modules?
>
> Any assistance would be greatly appreciated.

What you want is modules, which in short are files containing perl code
that can be imported into your main program with the use() and require()
commands.

See the perlmod and perlmodlib manual pages.

Joost.

Re: how to put subroutines in separate files and call?

am 18.01.2008 19:07:35 von jurgenex

birdsong9@gmail.com wrote:
>I'm a novice perl programmer but have been thinking about this
>question for a long time and finally decided to see if I could get
>some advice... I have a program that is several thousand lines of
>code, mostly broken up into dozens of subroutines, and if it's
>possible to do so would like to be able to call a subroutine that is
>stored in a separate file location and have it share data back and
>forth with my program. Is it possible to do this

That's what modules are for.

> without creating modules?

Well, you could use e.g. do(), but modules are the better method.

jue

Re: how to put subroutines in separate files and call?

am 18.01.2008 19:12:12 von birdsong9

On Jan 18, 10:07=A0am, Jürgen Exner wrote:
> birdso...@gmail.com wrote:
> >I'm a novice perl programmer but have been thinking about this
> >question for a long time and finally decided to see if I could get
> >some advice... I have a program that is several thousand lines of
> >code, mostly broken up into dozens of subroutines, and if it's
> >possible to do so would like to be able to call a subroutine that is
> >stored in a separate file location and have it share data back and
> >forth with my program. =A0Is it possible to do this
>
> That's what modules are for.
>
> > without creating modules?
>
> Well, you could use e.g. do(), but modules are the better method.
>
> jue

Thanks- just hoping there was a more straightforward way to do this.
For me, modules seem daunting. I appreciate the quick feedback.

Re: how to put subroutines in separate files and call?

am 18.01.2008 19:36:39 von smallpond

On Jan 18, 12:44 pm, birdso...@gmail.com wrote:
> I'm a novice perl programmer but have been thinking about this
> question for a long time and finally decided to see if I could get
> some advice... I have a program that is several thousand lines of
> code, mostly broken up into dozens of subroutines, and if it's
> possible to do so would like to be able to call a subroutine that is
> stored in a separate file location and have it share data back and
> forth with my program. Is it possible to do this without creating
> modules?
>
> Any assistance would be greatly appreciated.


modules are not as hard as you probably think they are.
Put one sub in a file foo.pm and add 'use foo;' in your
main program. Edit it while reading the perlmod docs
and just do the minumum to get it working. Post here if
you get stuck.

If you are using global variables in your subroutines then
1) you shouldn't be, and
2) you need to declare them 'our' instead of 'my'
to use them from a different package as $main::myglobal;

Re: how to put subroutines in separate files and call?

am 18.01.2008 19:54:57 von birdsong9

On Jan 18, 10:36=A0am, smallpond wrote:
> On Jan 18, 12:44 pm, birdso...@gmail.com wrote:
>
> > I'm a novice perl programmer but have been thinking about this
> > question for a long time and finally decided to see if I could get
> > some advice... I have a program that is several thousand lines of
> > code, mostly broken up into dozens of subroutines, and if it's
> > possible to do so would like to be able to call a subroutine that is
> > stored in a separate file location and have it share data back and
> > forth with my program. =A0Is it possible to do this without creating
> > modules?
>
> > Any assistance would be greatly appreciated.
>
> modules are not as hard as you probably think they are.
> Put one sub in a file foo.pm and add 'use foo;' in your
> main program. =A0Edit it while reading the perlmod docs
> and just do the minumum to get it working. =A0Post here if
> you get stuck.
>
> If you are using global variables in your subroutines then
> =A0 1) you shouldn't be, and
> =A0 2) you need to declare them 'our' instead of 'my'
> to use them from a different package as $main::myglobal;

Thanks for the encouragement- I'll give it a shot.

Re: how to put subroutines in separate files and call?

am 19.01.2008 07:47:31 von Ben Morrow

Quoth smallpond :
> On Jan 18, 12:44 pm, birdso...@gmail.com wrote:
>
> modules are not as hard as you probably think they are.
> Put one sub in a file foo.pm and add 'use foo;' in your

Don't use lowercase module names. They are reserved for pragmata. You
also want to think a little about a sensible name: try and fit it into
the CPAN hierarchy, as then the chance of name conflicts is smaller.

Ben