Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Simple: How to include file? (http://www.velocityreviews.com/forums/t870082-simple-how-to-include-file.html)

Tvrtko Ursulin 07-11-2003 01:22 PM

Simple: How to include file?
 

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?


Eric Wilhelm 07-11-2003 04:03 PM

Re: Simple: How to include file?
 
On Fri, 11 Jul 2003 08:56:40 -0500, Greg Bacon wrote:

> In article <20030711132522.1D889943.NOFFLE@orion.local>,
> Tvrtko Ursulin <tvrtko@croadria.com> wrote:
>
> : 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

Eric J. Roode 07-11-2003 11:22 PM

Re: Simple: How to include file?
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tvrtko Ursulin <tvrtko@croadria.com> wrote in
news:20030711132522.1D889943.NOFFLE@orion.local:

>
> 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+1O8Ql shc5sAoOLO
sPi5oTJMy8/N2EYAwnlQYbCh
=Dj98
-----END PGP SIGNATURE-----


All times are GMT. The time now is 08:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.