Simple: How to include file?

Discussion in 'Perl Misc' started by Tvrtko Ursulin, Jul 11, 2003.

  1. 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?
     
    Tvrtko Ursulin, Jul 11, 2003
    #1
    1. Advertising

  2. Tvrtko Ursulin

    Eric Wilhelm Guest

    On Fri, 11 Jul 2003 08:56:40 -0500, Greg Bacon wrote:

    > In article <>,
    > Tvrtko Ursulin <> 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 Wilhelm, Jul 11, 2003
    #2
    1. Advertising

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

    Tvrtko Ursulin <> wrote in
    news::

    >
    > 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-----
     
    Eric J. Roode, Jul 12, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov

    #include "file" -vs- #include <file>

    Victor Bazarov, Mar 5, 2005, in forum: C++
    Replies:
    4
    Views:
    552
    Exits Funnel
    Mar 6, 2005
  2. PTM
    Replies:
    1
    Views:
    340
    Andy Dingley
    Nov 12, 2007
  3. naveeddil
    Replies:
    0
    Views:
    592
    naveeddil
    Jan 4, 2008
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    967
    Andreas Bogenberger
    Feb 22, 2008
  5. Eric
    Replies:
    10
    Views:
    484
Loading...

Share This Page