lmarecha has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

This is my first post. I have already done some simple program but I have to do a more complete one and I was looking for a way to use a definition file. I was looking for an INCLUDE equivalent in Perl. What I want to put inside are subroutine and global variable definition.

It's something new to me, I check the FAQ but didn't find anything. Seems to be a basic question. Here is what I have find on different source but there is there something more simple to do ?

- Use package/module with require/use.

- Use eval to read a file and eval the content.

Cheers,

lmarecha

  • Comment on Is there any INCLUDE equivalent in Perl ?

Replies are listed 'Best First'.
Re: Is there any INCLUDE equivalent in Perl ?
by danger (Priest) on Feb 28, 2001 at 02:42 UTC

    As others have said, check out the do, require, and use documentation. I sometimes just make the config file a simple anonymous hash and then grab it via require()'s return value:

    # example program: #!/usr/bin/perl -w use strict; my $config = require("config.txt"); print "$config->{var1} $config->{var2}\n"; __END__ # and here's config.txt: { var1 => 'Hello', var2 => 'World', }
Re: Is there any INCLUDE equivalent in Perl ?
by archon (Monk) on Feb 28, 2001 at 02:31 UTC
    well, you have included (no pun intended) most of the methods to accomplish this task. you did leave out do 'file.pl', though. this is a basic question and is covered in the perlfunc manpage. why are you looking for simpler methods? these are pretty straightforward.

    incidentally, eval is probably the wrong thing to do in your case. you should choose one of 'do,' 'require,' or 'use.'

Re: Is there any INCLUDE equivalent in Perl ?
by mirod (Canon) on Feb 28, 2001 at 02:36 UTC

    Remember TMTOWTDI... there are (at least) 2 other methods:

    • do EXPR uses the value of EXPR as a filename and executes the contents of the file as a Perl script.
    • the -P command-line switch runs the C pre-processor on the file so you can actually use a good'ole #include file.

    A module is probably the cleaner and more Perlish way to handle this though.

Re: Is there any INCLUDE equivalent in Perl ?
by Hot Pastrami (Monk) on Feb 28, 2001 at 02:29 UTC
    Not that I advocate this way of accomplishing the task (I personally despise global variables being set in an external file), but useing a config file can be as simple as this:
    # filename: config $var1 = "First value"; $var2 = "Second value";
    ...and then in your script:
    use config; print "$var1 and $var2";

    Hot Pastrami
(boo)Re: Is there any INCLUDE equivalent in Perl ?
by boo_radley (Parson) on Feb 28, 2001 at 03:00 UTC
    Remember that when you're splitting functions off into their own library and useing or requireing them, that those libraries must explicitly return true.
    putting
    return 1; or
    return "true"; or even
    return "cheese sandwich and pie";
    will ensure that your module doesn't cause any problems when you attempt to compile your projects.
    update as tye pointed out, the return may be implicit -- you may omit the return from the above lines.
      Just noticed it (before doing the mistake).
      I think I will go for the use of the 'use' function. I don't know why but I always thinked (until now) that the 'use' was only for module and package with the right declaration header.

      lmarecha