Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > include external perl program

Reply
Thread Tools

include external perl program

 
 
Alythh
Guest
Posts: n/a
 
      03-08-2005
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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Alythh) wrote in news:6a25ba72.0503080906.11cad2f9
@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.
 
Reply With Quote
 
 
 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      03-08-2005
Alythh wrote:
>
> 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

 
Reply With Quote
 
mazzawi@gmail.com
Guest
Posts: n/a
 
      03-08-2005
"They work fine for simple applications"
why would you just use 'do $File' for simple applications?

 
Reply With Quote
 
alythh@netscape.net
Guest
Posts: n/a
 
      03-09-2005
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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-09-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:

> I still have some problems on scoping...



Then see:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl / cgi / include file a la #include me Perl Misc 10 03-26-2010 10:21 PM
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
How to include an external (binary) file into a java program at compile (!) time ??? Tobias Merler Java 16 10-03-2004 11:35 AM
How to structure a perl program to include and exclude files? Henry Law Perl Misc 3 07-22-2004 06:45 PM
Include data in a perl program? David O in Seattle Perl Misc 8 02-13-2004 06:54 PM



Advertisments