include external perl program

A

Alythh

I need to use an input file to a main perl program.
Since I'm changing its shape often, and I'd like to add comments here
and there, I was thinking to giving it the shape of a perl program by
itself, like this:

# comment
# ...
$inputvar1=2;
# comment
$inputvar2=2;
....

and to include it some way in the main program.
Problem is, I don't know which command is used to <include> a file.
I heard about "use" and "require", but it seems to me that they're
mostly used for defining big libraries and/or OOprogramming, isnt it?

any hint?

thanks!

Alessandro Magni
 
A

A. Sinan Unur

(e-mail address removed) (Alythh) wrote in @posting.google.com:
I need to use an input file to a main perl program.
Since I'm changing its shape often, and I'd like to add comments here
and there, I was thinking to giving it the shape of a perl program by
itself, like this:

# comment
# ...
$inputvar1=2;
# comment
$inputvar2=2;
...

The design choice depends on the purpose of this file. Is it used to
tune some configuration parameters? In that case, I myself would have
used a regular configuration file format rather than Perl code. There
are many modules on CPAN that deal with configuration files in various
formats. One particularly simple one is Config::Auto.
and to include it some way in the main program.
Problem is, I don't know which command is used to <include> a file.

perldoc -f do
perldoc -f require
perldoc -f use
I heard about "use" and "require", but it seems to me that they're
mostly used for defining big libraries and/or OOprogramming, isnt it?

A module need not be "big" or OO.

perldoc perlmod

Sinan.
 
J

jl_post

Alythh said:
I was thinking to giving it the shape of a perl program
by itself, like this:

# comment
# ...
$inputvar1=2;
# comment
$inputvar2=2;
...

and to include it some way in the main program.


Ciao, Alessandro!

If I understand you correctly, you want to be able to have a main
program file, and to be able to specify several variables in a separate
file.

If this is what you want, you can do so like this:

Create a file called "inputvars.pm" that looks like this:

====== START OF "inputvars.pm" =======
# This file is for defining input variables.
use strict;
use warnings;
# $inputvar1 is for ...
our $inputvar1 = 2;
# $inputvar2 is for ...
our $inputvar2 = 2;
1;
====== END OF "inputvars.pm" =======

Then create a main Perl program, like this:

====== START OF "script.pl" =======
#!/usr/bin/perl
use strict;
use warnings;

use inputvars;

our ($inputvar1, $inputvar2);

print "Value of \$inputvar1: $inputvar1\n";
print "Value of \$inputvar2: $inputvar2\n";

__END__
====== END OF "script.pl" =======

Then run script.pl with a command like:

perl script.pl

Ecco fatto! Now the values you defined in inputvars.pm show up in
script.pl !

If you use this approach, there are a few things you need to
remember:

1. DON'T name your module file "vars.pm" (because
there already is a module with that name).
2. The last line of your module file should always
end with the line:
1;
Not doing so may give you an error.
3. Declare any shared variables (variables whose values
are defined in one file and used in another) with
"our" (and not "my").
4. It is recommended that you always "use strict;" and
"use warnings;" in all your files. Doing so will
help you catch many errors that you didn't expect
to have.

Spero di averti aiutato, Alessandro.

-- Jean-Luc Romano
 
M

mazzawi

"They work fine for simple applications"
why would you just use 'do $File' for simple applications?
 
A

alythh

thank you all people,

<I always find the Perl community the most helpful and friendly around
the web, it must be something in the language itself?>

I found the do $cfgfile the easiest and most straightforward - I didnt
know this form of do, and had some problems at first since I used 'my'
instead of 'our' - I still have some problems on scoping...

thank you all!

Alessandro
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top