Simple: How to include file?

T

Tvrtko Ursulin

Hello everyone!

I am new to perl (from C) and cannot figure how to include a simple custom
file in the main program?

I need something like:

our $default_value = 'xyz';

in my include.ph, and a way to include this in main.pl so that
$default_value can normally be used. Can it be done?
 
E

Eric Wilhelm

: I am new to perl (from C) and cannot figure how to include a simple :
custom file in the main program?
:
: I need something like:
:
: our $default_value = 'xyz';
:
: in my include.ph, and a way to include this in main.pl so that :
$default_value can normally be used. Can it be done?

You could do it quickly with

% cat try
#! /usr/local/bin/perl

require "foo.pl";

print $default_value, "\n";
% cat foo.pl
$default_value = 'xyz';
% ./try
xyz

Depending on the nature of your project, you probably don't want to do
it that way, though, but want instead to write a module.

I second that

There is also:
do "foo.pl"

which eases some of the restrictions (and can be called multiple times),
but makes debugging more difficult if there is something wrong with foo.pl
(you won't get any compiler warnings about the file.)

This is a very duct-tape and twine approach (doing it with a require is
not much better.) If you are working on something that is going to grow,
definitely avoid it (feeling the pain right now myself.)


--Eric
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello everyone!

I am new to perl (from C) and cannot figure how to include a simple
custom file in the main program?

I need something like:

our $default_value = 'xyz';

in my include.ph, and a way to include this in main.pl so that
$default_value can normally be used. Can it be done?

I just wrote a module a few weeks ago to do this very thing. See
Config::Vars on CPAN. You would use it more or less like this:

# In file "My_vars.pm":
use strict;
package My_vars;
use Config::Vars;

var $default_value = 'xyz';
# File My_vars.pm ends


# In some other file:
use strict;
use My_vars qw($default_value);

print $default_value;
# other file ends

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPw9GzmPeouIeTNHoEQKPJQCfTLS3vNxlhZRuKK1q+1O8Qlshc5sAoOLO
sPi5oTJMy8/N2EYAwnlQYbCh
=Dj98
-----END PGP SIGNATURE-----
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top