include external perl program

Discussion in 'Perl Misc' started by Alythh, Mar 8, 2005.

  1. Alythh

    Alythh Guest

    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
    Alythh, Mar 8, 2005
    #1
    1. Advertising

  2. (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.
    A. Sinan Unur, Mar 8, 2005
    #2
    1. Advertising

  3. Alythh

    Guest

    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
    , Mar 8, 2005
    #3
  4. Alythh

    Guest

    "They work fine for simple applications"
    why would you just use 'do $File' for simple applications?
    , Mar 8, 2005
    #4
  5. Alythh

    Guest

    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
    , Mar 9, 2005
    #5
  6. Tad McClellan, Mar 9, 2005
    #6
    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. Tobias Merler
    Replies:
    16
    Views:
    1,258
    Yakov
    Oct 3, 2004
  2. Andreas Bogenberger
    Replies:
    3
    Views:
    899
    Andreas Bogenberger
    Feb 22, 2008
  3. Gunnar Hjalmarsson

    Perl and waiting for execution of external program

    Gunnar Hjalmarsson, Jan 19, 2005, in forum: Perl Misc
    Replies:
    11
    Views:
    357
    Anno Siegel
    Jan 21, 2005
  4. Replies:
    9
    Views:
    286
    Kiss Gabor
    Dec 31, 2008
  5. me
    Replies:
    10
    Views:
    969
    ccc31807
    Mar 26, 2010
Loading...

Share This Page