Home > Archive > PERL Beginners > April 2005 > Script that spans multiple files
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Script that spans multiple files
|
|
| Daniel Kasak 2005-04-21, 3:56 am |
| Hi all.
I have a large script ( 6000 lines ) that I'd like to break into logical
units. Is there a way I can tell perl to 'append' a list of files into 1
script so that I can call subs from any of the files and have them
treated as if they were all from the same script ( ie all my variables
will be visible )?
Thanks :)
--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: dkasak@nusconsulting.com.au
website: http://www.nusconsulting.com.au
| |
| Ramprasad A Padmanabhan 2005-04-21, 8:55 am |
| On Thu, 2005-04-21 at 11:09, Daniel Kasak wrote:
> Hi all.
>
> I have a large script ( 6000 lines ) that I'd like to break into logical
> units. Is there a way I can tell perl to 'append' a list of files into 1
> script so that I can call subs from any of the files and have them
> treated as if they were all from the same script ( ie all my variables
> will be visible )?
>
> Thanks :)
Dont do that,
IMHO creating a script at runtime and executing is plain stupid.
Debugging will be a nightmare and so will maintenance be.
Why cant you divide the logical units and put them into functions , in
different library files , if necessary. Pass all variables as function
arguments.
Thanks
Ram
----------------------------------------------------------
Netcore Solutions Pvt. Ltd.
Website: http://www.netcore.co.in
Spamtraps: http://cleanmail.netcore.co.in/directory.html
----------------------------------------------------------
| |
| Charles K. Clarkson 2005-04-21, 8:55 am |
| Daniel Kasak <mailto:dkasak@nusconsulting.com.au> wrote:
:
: I have a large script ( 6000 lines ) that I'd like to break into
: logical units. Is there a way I can tell perl to 'append' a list
: of files into 1 script so that I can call subs from any of the
: files and have them treated as if they were all from the same
: script ( ie all my variables will be visible )?
If I understand you correctly, yes, but what would be the
point? 6000 lines distributed over several files which, when
called, are combined back into a 6000 line script doesn't really
do anything. The result is the same: a 6000 line script.
Better to use modules or objects. You might start by reading
the perl docs: 'perlmod' and 'perlmodstyle' are good places to
start.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
| |
| Offer Kaye 2005-04-21, 8:55 am |
| On 4/21/05, Daniel Kasak wrote:
> Hi all.
>=20
> I have a large script ( 6000 lines ) that I'd like to break into logical
> units. Is there a way I can tell perl to 'append' a list of files into 1
> script so that I can call subs from any of the files and have them
> treated as if they were all from the same script ( ie all my variables
> will be visible )?
>=20
It's called a "Perl Module", or .pm file. See:
perlmod Perl modules: how they work
perlmodlib Perl modules: how to write and use
perlmodstyle Perl modules: how to write modules with style
Brief example - say you have a module file called Foo.pm with the
following contents:
####### begin Foo.pm contents
package Foo;
use Exporter qw( import );
@EXPORT_OK =3D qw(munge); # symbols to export on request
use strict;
use warnings;
our $VERSION =3D '0.01';
sub munge {
print "This is a sub!\n";
}
1;
####### end Foo.pm contents
Now in another script, say use_foo.pl, you write:
####### begin use_foo.pl contents
use strict;
use warnings;
use Foo qw(munge);
print "Foo::VERSION =3D $Foo::VERSION\n";
munge();
####### end use_foo.pl contents
$VERSION is an example of a variable in Foo.pm accessible from an
external script, and munge() is an example of such a function.
HTH,
--=20
Offer Kaye
| |
| Daniel Kasak 2005-04-22, 3:55 am |
| Charles K. Clarkson wrote:
>Daniel Kasak <mailto:dkasak@nusconsulting.com.au> wrote:
>:
>: I have a large script ( 6000 lines ) that I'd like to break into
>: logical units. Is there a way I can tell perl to 'append' a list
>: of files into 1 script so that I can call subs from any of the
>: files and have them treated as if they were all from the same
>: script ( ie all my variables will be visible )?
>
>
> If I understand you correctly, yes, but what would be the
>point? 6000 lines distributed over several files which, when
>called, are combined back into a 6000 line script doesn't really
>do anything. The result is the same: a 6000 line script.
>
>
>
Managability. I can put all related stuff in 1 file. Then I'll have main
application logic in one main file, and other subs in other files. It
reduces clutter. Yes I realise that what I'm asking for would have no
difference to the Perl interpreter ... that's the point.
> Better to use modules or objects. You might start by reading
>the perl docs: 'perlmod' and 'perlmodstyle' are good places to
>start.
>
And yes I know about modules / objects. I've made a couple of OS modules
myself. What I want here is part-way to a module. My main issue is that
I want of all my variables to be visible without having to pass them in
to each sub. There are a lot of variables and a lot of subs.
--
Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2 9922 7989
email: dkasak@nusconsulting.com.au
website: http://www.nusconsulting.com.au
| |
| 1911que@gmail.com 2005-04-22, 3:55 am |
| Daniel Kasak wrote:[color=darkred]
> Charles K. Clarkson wrote:
>
>
Not so sure about that. I have a script that I broke into 100 different
files and doing so, I increased my speed from system 1.30 seconds down
to system 0.22. (That is shaving 1.1 seconds off...)
In the original script, I had a total of 8,000 lines of code (including
"required" files)
Now instead of the entire script being called, only the main.pl file and
the needed sub file is executed.
| |
| Charles K. Clarkson 2005-04-22, 3:55 am |
| Daniel Kasak <mailto:dkasak@nusconsulting.com.au> wrote:
: Charles K. Clarkson wrote:
: Managability. I can put all related stuff in 1 file. Then I'll
: have main application logic in one main file, and other subs in
: other files. It reduces clutter. Yes I realise that what I'm
: asking for would have no difference to the Perl interpreter ...
: that's the point.
You can 'require' your files.
use lib '.'; # Point to the path of your other files.
require 'foo.pl';
require 'bar.pl';
require 'baz.pl';
If you have a long list of files, you could use a file to
store your included files.
includes.pl:
use lib '.'; # Point to the path of your other files.
require 'foo.pl';
require 'bar.pl';
require 'baz.pl';
__END__
main_script.pl:
require 'includes.pl';
: And yes I know about modules / objects. I've made a couple of OS
: modules myself. What I want here is part-way to a module. My
: main issue is that I want of all my variables to be visible
: without having to pass them in to each sub.
Whoa, Bucko! Let's not be talking about those kinds of
variables. I just had supper and I don't want it coming back up.
Four out of five dentists recommend you avoid the kind of
variables you have in your script. Still, sometimes its easier not
to rewrite the entire script. Good Luck.
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328
| |
| M. Kristall 2005-04-22, 8:55 am |
| Charles K. Clarkson wrote:
> Daniel Kasak <mailto:dkasak@nusconsulting.com.au> wrote:
> :
> : I have a large script ( 6000 lines ) that I'd like to break into
> : logical units. Is there a way I can tell perl to 'append' a list
> : of files into 1 script so that I can call subs from any of the
> : files and have them treated as if they were all from the same
> : script ( ie all my variables will be visible )?
>
>
> If I understand you correctly, yes, but what would be the
> point? 6000 lines distributed over several files which, when
> called, are combined back into a 6000 line script doesn't really
> do anything. The result is the same: a 6000 line script.
The point is simple: upkeep. If you have a 6000 line script, it will be
harder to find the specific part you want to fix, it will be much easier
with separate, intelligently organized files than a single file.
This is especially helpful with multiple developers.
>
> Better to use modules or objects. You might start by reading
> the perl docs: 'perlmod' and 'perlmodstyle' are good places to
> start.
>
> HTH,
>
> Charles K. Clarkson
|
|
|
|
|